0% found this document useful (0 votes)
26 views3 pages

Lab Report#10: Title

This lab report discusses two examples of using Transact-SQL functions. The first example creates a table-valued function called fn_ProductSales that returns sales data for a given product ID from two tables. The second example creates a scalar function called fn_MaxValue that returns the maximum of two input values or returns a message if they are equal. Both functions are created and executed to demonstrate their functionality.

Uploaded by

malik Arsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views3 pages

Lab Report#10: Title

This lab report discusses two examples of using Transact-SQL functions. The first example creates a table-valued function called fn_ProductSales that returns sales data for a given product ID from two tables. The second example creates a scalar function called fn_MaxValue that returns the maximum of two input values or returns a message if they are equal. Both functions are created and executed to demonstrate their functionality.

Uploaded by

malik Arsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab Report#10

Database Management System

Title: FUNCTIONS (Transact-SQL)

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;

You might also like