Lab Report#10: Title
Lab Report#10: Title
Submitted To:
Sir Shahid Ali
Submitted By:
Muhammad Bilal
17-CP-52
Dated: 17/07/2020
Task/Example#01:
Table 1:
(tb_Sales_L9T1)
Table 2:
(tb_Products_L9T1)
query_fn_ProductSales_T1.sql:
CREATE FUNCTION fn_ProductSales (@prod_id int)
RETURNS TABLE
AS
RETURN
(
SELECT P.P_Id, P.P_Name, S.Customer, SUM(S.S_Price) AS 'YTD Totals'
FROM tb_Products_L9T1 AS P JOIN tb_Sales_L9T1 AS S
ON S.P_id = P.P_ID
WHERE S.P_id = @prod_id
Group by P.P_ID, P.P_Name, S.customer
);
GO
query_fn_ProductSales_exec_T1.sql:
SELECT * FROM fn_ProductSales (1);
Task/Example 2:
query_fn_MaxValue_T2.sql:
CREATE FUNCTION fn_MaxValue(@VAL1 int, @VAL2 int)
RETURNS varchar(30)
AS
BEGIN
if @val1 > @val2 return @val1
if @VAL1 < @VAL2 return @val2
if @VAL1 = @VAL2 return 'values are equal' --Or you can print something using following statment:-
--return cast('Error happened here.' as CHAR(100))
return isnull(@val2,@val1)
END
GO
query_fn_MaxValue_exec_T2.sql:
select dbo.fn_MaxValue (7,3) AS Result;