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 .

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.

thnx for the help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.