0

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 .

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by y2kshane
1

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.