Wednesday, February 10, 2010

More functions

Select dbo.func_cube(SubscriptTypeID) from MagazineDetail

Alter function func_SubscriptionEndDate
--parameters entered by the user
(@MagDetID int, @StartDate datetime)
Returns datetime --return type
As
Begin
Declare @type int --declare a variable to get the subscription type
Select @type = SubscriptTypeID from MagazineDetail
Where magDetID=@magDetID --query magazinedetail table to get subscription type
Declare @enddate Datetime --declare variable for end date
Select @enddate =
Case --choose end date based on subscription type
When @type=1 then dateadd(mm,6,@startdate)
When @type=2 then dateadd(yy,1,@startDate)
When @type=3 then dateadd(mm,3,@startDate)
When @type=4 then dateadd(yy,2,@startDate)
When @type=5 then dateadd(yy,5,@startDate)
When @type=6 then dateadd(yy,3,@startDate)
Else GetDate()
end
Return (@EndDate)--return the resulting endate
End

Select dbo.func_SubscriptionEndDate(1, GetDate())

Select * from MagazineDetail

Insert into Subscription(CustID, MagDetID, SubscriptionStart, SubscriptionEnd)
Values(2,6,GetDate(), dbo.func_SubscriptionEndDate(6,GetDate()))

Select * from Subscription where custid=2


Alter function func_SalesTax
(@saleprice money, @rate decimal(6,3))
--rate as decimal
returns money
As
Begin
if @rate >=1
Begin
set @rate=@rate/100
End
Declare @tax money
set @Tax=@salePrice * @rate
Return @Tax
end



Select SubscriptionPrice, dbo.func_SalesTax(SubscriptionPrice, 1212.00) as tax from MagazineDetail

No comments:

Post a Comment