954,560 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

SQL function problem

hey how to answer to these questions


Table: Sales(CustomerID , RepID , Area , SaleDate , ProductNo , Quantity , UnitPrice , ItemTotalPrice)

a)considering the above table write a function to calculate the IemTotalPrice as
ItemTotalPrice=Quantity*UnitPrice


b)Write a SQL statement to create the above table
(you must use the above created function so that it automatically calculate the value for the field ItyemTotalPrice)

so far i only got answer to a) but i dont know its right or wrong
this is my answer

create function calc (@qty int,@up int) return int
as
declare @ItemTotalPrice int 
set @ItemTotalPrice=@qty*@up
return @ItemTotalPrice


i dont know how to answer to b) plz help me .

y2kshane
Light Poster
35 posts since Aug 2010
Reputation Points: 8
Solved Threads: 0
 

For part A, they are wanting a function that uses the specific fields in your table, not passes them as parameters to the function. Your function will probably look something like this:

-- give the function a meaningful name, and return an appropriate data type
CREATE FUNCTION GetItemTotalPrice() RETURN MONEY
AS
DECLARE @TotalPrice MONEY
-- total price is defined as quantity x unitprice from the sales table
SELECT @TotalPrice = Quantity * UnitPrice FROM Sales
RETURN @TotalPrice


For B you will need to do a CREATE TABLE statement. Here is a link to the MSDN documentation for such a statement, there are also many many examples online. Have a try and repost if you need more help, along with your attempt.

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

thnx for the help :)

y2kshane
Light Poster
35 posts since Aug 2010
Reputation Points: 8
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: