Repository navigation
Expand file tree
/
Copy path24_StoredProcedures_Solutions.sql
More file actions
106 lines (70 loc) · 2.68 KB
/
Copy path24_StoredProcedures_Solutions.sql
File metadata and controls
106 lines (70 loc) · 2.68 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/******************************************************************************
Course videos: https://www.red-gate.com/hub/university/courses/t-sql/tsql-for-beginners
Course scripts: https://litknd.github.io/TSQLBeginners
Stored Procedures
SOLUTIONS
*****************************************************************************/
/* ✋🏻 Doorstop ✋🏻 */
RAISERROR(N'Did you mean to run the whole thing?', 20, 1) WITH LOG;
GO
USE WideWorldImporters;
GO
/* Q1
Create a procedure named dbo.MostRecentInvoiceIDForCustomerID with two parameters:
@CustomerID INT,
@InvoiceID INT OUTPUT
The query should return the most recent InvoiceID for that CustomerID based on the
InvoiceDate column
Write code to execute the procedure and return the most recent InvoiceID for @CustomerID = 1
The result should be InvoiceID 70232
*/
CREATE OR ALTER PROCEDURE dbo.MostRecentInvoiceIDForCustomerID
@CustomerID INT,
@InvoiceID INT OUTPUT
AS
SELECT TOP (1)
@InvoiceID = InvoiceID
FROM Sales.Invoices
WHERE CustomerID = @CustomerID
ORDER BY InvoiceDate DESC;
GO
DECLARE @InvoiceIDOUT INT;
EXEC dbo.MostRecentInvoiceIDForCustomerID @CustomerID = 1,
@InvoiceID = @InvoiceIDOUT OUT;
SELECT @InvoiceIDOUT AS InvoiceIDOUT;
GO
/* Q2
This question builds on the prior question
Create a procedure named dbo.MostRecentInvoiceLinesForCustomerID with one parameter:
@CustomerID INT
This procedure should call the dbo.MostRecentInvoiceIDForCustomerID procedure to get
the most recent InvoiceID
It should then query the Sales.InvoiceLines table and return a dataset containing the
following columns:
InvoiceID,
InvoiceLineID,
Description,
Quantity,
UnitPrice
Write code to execute dbo.MostRecentInvoiceIDForCustomerID and return the most recent dataset for CustomerID = 1
The result set should look like this:
InvoiceID InvoiceLineID Description Quantity UnitPrice
--------- ------------- ---------------------------------- -------- ---------
70232 227357 Tape dispenser (Black) 50 32.00
70232 227358 10 mm Double sided bubble wrap 20m 20 30.00
*/
CREATE OR ALTER PROCEDURE dbo.MostRecentInvoiceLinesForCustomerID @CustomerID INT
AS
DECLARE @InvoiceIDOUT INT;
EXEC dbo.MostRecentInvoiceIDForCustomerID @CustomerID = @CustomerID,
@InvoiceID = @InvoiceIDOUT OUT;
SELECT @InvoiceIDOUT AS InvoiceID,
InvoiceLineID,
[Description],
Quantity,
UnitPrice
FROM Sales.InvoiceLines
WHERE InvoiceID = @InvoiceIDOUT;
GO
EXEC dbo.MostRecentInvoiceLinesForCustomerID @CustomerID = 1;
GO