-
Notifications
You must be signed in to change notification settings - Fork 0
/
CIS 310 SQL Problems 14 Stored Procedure Queries.sql
75 lines (63 loc) · 1.91 KB
/
CIS 310 SQL Problems 14 Stored Procedure Queries.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
-- Rob DeVoto
--CIS 310
-- Fall 2022
-- Problems 14 - Stored Procedure Queries
-- Uses Staywell_DB_Script
-- DO NOT NEED TO END STORED PROCEDURE CODE WITH A ; LIKE OTHER CODE
-- CREATE A STORED PROCEDURE
CREATE PROCEDURE REPS_CUSTOMER_LIST @REP_ID CHAR(2) = NULL
AS
BEGIN
SELECT CUST_ID, LAST_NAME+', '+FIRST_NAME AS CUST_NAME -- CAN ALSO USE CONCAT( , , )
FROM CUSTOMER
WHERE REP_ID = @REP_ID
END
-- RUN THE STORED PROCEDURE
EXEC REPS_CUSTOMER_LIST'10'
EXEC REPS_CUSTOMER_LIST
-- ALTER THE STORED PROCEDURE, MAKE IT BETTER TO ACCOUNT FOR NULL VALUES
ALTER PROCEDURE REPS_CUSTOMER_LIST @REP_ID CHAR(2) = NULL
AS
BEGIN
IF @REP_ID IS NULL
BEGIN
PRINT 'REP ID not provided'
END
ELSE
SELECT CUST_ID, LAST_NAME+', '+FIRST_NAME AS CUST_NAME -- CAN ALSO USE CONCAT( , , )
FROM CUSTOMER
WHERE REP_ID = @REP_ID
END
-- RUN THE STORED PROCEDURE
EXEC REPS_CUSTOMER_LIST'10'
EXEC REPS_CUSTOMER_LIST
-- UPDATE A STORED PROCEDURE
CREATE PROCEDURE CHG_CUST_LAST_NAME
@CUST_ID CHAR(3),
@NEWLASTNAME VARCHAR(20)
AS
BEGIN
UPDATE CUSTOMER
SET LAST_NAME = @NEWLASTNAME
WHERE CUST_ID = @CUST_ID
END
-- CHECK AND RUN THE UPDATED STORED PROCEDURE, SEE CHANGES
SELECT * FROM CUSTOMER
EXEC CHG_CUST_LAST_NAME '721', 'Jackson'
SELECT * FROM CUSTOMER
-- DELETE STORED PROCEDURE - DELETES A RECORD, NOT A WHOLE PROCEDURE
CREATE PROCEDURE DEL_INVOICE
@INVOICE_NUM CHAR(5)
AS
BEGIN
DELETE
FROM INVOICE_LINE
WHERE INVOICE_NUM = @INVOICE_NUM --FOREIGN KEY IN IVOICE_LINE TABLE, SO MUST DELETE FROM HERE FIRST DUE TO REFERENTIAL INTEGRITY
DELETE
FROM INVOICES
WHERE INVOICE_NUM = @INVOICE_NUM --PRIMARY KEY IN IVOICE_LINE TABLE
END
-- RUN THE NEW DELETE STORED PROCEDURE
SELECT * FROM INVOICES I INNER JOIN INVOICE_LINE IL ON I.INVOICE_NUM = IL.INVOICE_NUM;
EXEC DEL_INVOICE '14216'
SELECT * FROM INVOICES I INNER JOIN INVOICE_LINE IL ON I.INVOICE_NUM = IL.INVOICE_NUM;