0% found this document useful (0 votes)
54 views

This Study Resource Was: Assignment - Stored Procedure and UDF

The document provides instructions for creating 4 SQL scripts: 1) A stored procedure that displays customer information based on a state code parameter. 2) A stored procedure that displays sales transaction details based on a transaction number parameter. 3) A user-defined function that returns the total payment for a transaction based on the transaction number. 4) A stored procedure that displays transaction number, customer name, total sales, total payment, and difference between sales and payment for each transaction without a parameter.

Uploaded by

Marvin Cinco
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

This Study Resource Was: Assignment - Stored Procedure and UDF

The document provides instructions for creating 4 SQL scripts: 1) A stored procedure that displays customer information based on a state code parameter. 2) A stored procedure that displays sales transaction details based on a transaction number parameter. 3) A user-defined function that returns the total payment for a transaction based on the transaction number. 4) A stored procedure that displays transaction number, customer name, total sales, total payment, and difference between sales and payment for each transaction without a parameter.

Uploaded by

Marvin Cinco
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment - Stored Procedure and UDF

SQL SCRIPT 1: Create a stored procedure SHOWSTATE() that has a parameter of two-character state
code ( eg. NY, CA, NJ) that will list all information about the customer who lives on a particular state.
Refer CUSTOMER table. The state code can be in lower or uppercase supplied in the parameter. Use CA
to test your stored procedure.

--#SET TERMINATOR @

CREATE PROCEDURE SHOWSTATE ( Customer VARCHAR(2))

dynamic result sets 1

BEGIN

SET Customer = UPPER(Customer);

m
er as
BEGIN

co
eH w
DECLARE a1 CURSOR WITH RETURN FOR

o.
SELECT * FROM CUSTOMER
rs e
ou urc
WHERE ADDRESS LIKE ('%' || Customer ||'%');

OPEN a1;
o
aC s

END;
vi y re

END @

--#SET TERMINATOR ;
ed d
ar stu

Call SHOWSTATE('CA')
is
Th
sh

This study source was downloaded by 100000829178195 from CourseHero.com on 11-22-2021 01:44:17 GMT -06:00

https://www.coursehero.com/file/61592036/Stored-Procedure-and-UDFpdf/
m
er as
co
eH w
o.
Hint: Use LIKE and UPPER in your stored procedure.

rs e
ou urc
SQL SCRIPT 2: Formulate a stored procedure DISPLAYTRANS() that will display the corresponding sales
transaction based from transaction number passed. Display transaction number, sales date, product
code, description, unit, quantity. Use TR000039 to test the output of your script.
o

--#SET TERMINATOR @
aC s
vi y re

CREATE PROCEDURE DISPLAYTRANS (transNum VARCHAR(10))

DYNAMIC RESULT SETS 1


ed d

BEGIN
ar stu

BEGIN
is

DECLARE a1 CURSOR WITH RETURN FOR


Th

SELECT sd.transNo, s.salesDate, p.prodCode, p.description, p.unit,


sd.quantity
sh

FROM sales s

JOIN salesdetail sd ON s.transNo = sd.transNo

JOIN product p ON sd.prodCode = p.prodCode

This study source was downloaded by 100000829178195 from CourseHero.com on 11-22-2021 01:44:17 GMT -06:00

https://www.coursehero.com/file/61592036/Stored-Procedure-and-UDFpdf/
WHERE transNum = sd.transNo;

OPEN a1;

END;

END @

--#SET TERMINATOR ;

CALL DISPLAYTRANS('TR000039')

m
er as
co
eH w
o.
rs e
ou urc
o
aC s
vi y re
ed d
ar stu

SQL SCRIPT 3: Create a user-defined function GET_TOTAL_PAYMENT() that will get the total payment
made for each transaction. A transaction number parameter will be included when the function is
called. Use TR000092 to test your function.
is
Th

--#SET TERMINATOR @

CREATE FUNCTION GET_TOTAL_PAYMENT (transNum VARCHAR(10))


sh

RETURNS DOUBLE

NO EXTERNAL ACTION

BEGIN ATOMIC

This study source was downloaded by 100000829178195 from CourseHero.com on 11-22-2021 01:44:17 GMT -06:00

https://www.coursehero.com/file/61592036/Stored-Procedure-and-UDFpdf/
DECLARE total_payment DOUBLE;

SET total_payment = (SELECT SUM(amount)

FROM PAYMENT

WHERE transNum = transNo

GROUP BY 1);

RETURN total_payment;

END@

m
er as
--#SET TERMINATOR ;

co
eH w
SELECT DISTINCT GET_TOTAL_PAYMENT('TR000092') "TOTAL PAYMENT"

o.
FROM payment
rs e
ou urc
o
aC s
vi y re
ed d
ar stu
is
Th

SQL SCRIPT 4: Create a stored procedure PAYMENT_SHORT that will display the transaction number,
customer name, total sales, total payment, and the difference between total sales and total payment.
This stored procedure has no parameters.
sh

On this procedure you will use different sets of view that you have created to extract to total sales
depending on the correct unit price to be used based on effectivity date and sales date.

Create these views first before using it inside your stored procedure.NO NEED TO DISPLAY THE
CONTENT OF THESE VIEWS.

This study source was downloaded by 100000829178195 from CourseHero.com on 11-22-2021 01:44:17 GMT -06:00

https://www.coursehero.com/file/61592036/Stored-Procedure-and-UDFpdf/
To display output of the requirement, include GET_TOTAL_PAYMENT user-defined function that you
have created in item 3. TRANSNO should serve as the parameter AND NOT TR000092.

m
er as
co
eH w
o.
rs e
ou urc
o
aC s
vi y re
ed d
ar stu
is
Th
sh

This study source was downloaded by 100000829178195 from CourseHero.com on 11-22-2021 01:44:17 GMT -06:00

https://www.coursehero.com/file/61592036/Stored-Procedure-and-UDFpdf/
Powered by TCPDF (www.tcpdf.org)

You might also like