-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_Stored_Procedures.sql
More file actions
239 lines (216 loc) · 6.35 KB
/
05_Stored_Procedures.sql
File metadata and controls
239 lines (216 loc) · 6.35 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**************************************************************
* SQL Server 2022 Stored Procedures Tutorial
* Description: This script demonstrates creating stored
* procedures for various scenarios including
* simple queries, error handling, table-valued
* parameters (TVP), native compilation, output
* parameters, and handling XML/JSON inputs.
**************************************************************/
-------------------------------------------------
-- Region: 0. Initialization
-------------------------------------------------
/*
Ensure you're using the target database for stored procedure operations.
*/
USE TestDB;
GO
-------------------------------------------------
-- Region: 1. Simple Stored Procedure
-------------------------------------------------
/*
1.1 GetAnimalByName: Returns animal information based on a provided name.
*/
CREATE PROCEDURE GetAnimalByName
@Name NVARCHAR(60)
AS
BEGIN
SET NOCOUNT ON; -- Suppress unnecessary messages for performance.
SELECT [Name]
FROM dbo.Animals
WHERE [Name] = @Name;
END;
GO
-------------------------------------------------
-- Region: 2. Complex Stored Procedure with Error Handling
-------------------------------------------------
/*
2.1 AddAnimal: Inserts a new animal record and returns error details if insertion fails.
Note: Ensure the dbo.Animals table has columns [Name], [Type], and [Age].
*/
CREATE PROCEDURE AddAnimal
@Name NVARCHAR(60),
@Type NVARCHAR(60),
@Age INT
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
INSERT INTO dbo.Animals ([Name], [Type], [Age])
VALUES (@Name, @Type, @Age);
END TRY
BEGIN CATCH
-- Return detailed error information
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() AS ErrorState,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
END;
GO
-------------------------------------------------
-- Region: 3. Stored Procedure with Table-Valued Parameter (TVP)
-------------------------------------------------
/*
3.1 Create a table type to be used as a TVP.
*/
IF TYPE_ID(N'AnimalTableType') IS NOT NULL
DROP TYPE AnimalTableType;
GO
CREATE TYPE AnimalTableType AS TABLE
(
[Name] NVARCHAR(60),
[Type] NVARCHAR(60),
[Age] INT
);
GO
/*
3.2 AddAnimals: Inserts multiple animal records using a TVP.
*/
CREATE PROCEDURE AddAnimals
@Animals AnimalTableType READONLY
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO dbo.Animals ([Name], [Type], [Age])
SELECT [Name], [Type], [Age]
FROM @Animals;
END;
GO
-------------------------------------------------
-- Region: 4. Native Stored Procedure
-------------------------------------------------
/*
4.1 GetAnimalsByType: Retrieves animals by type using native compilation.
Note: Native compiled stored procedures require specific database settings.
*/
CREATE PROCEDURE GetAnimalsByType
@Type NVARCHAR(60)
WITH NATIVE_COMPILATION, SCHEMABINDING
AS
BEGIN ATOMIC WITH
(
TRANSACTION ISOLATION LEVEL = SNAPSHOT,
LANGUAGE = N'us_english'
)
SELECT [Name], [Age]
FROM dbo.Animals
WHERE [Type] = @Type;
END;
GO
-------------------------------------------------
-- Region: 5. Stored Procedure with Output Parameter
-------------------------------------------------
/*
5.1 GetAnimalCountByType: Returns the count of animals for a given type.
*/
CREATE PROCEDURE GetAnimalCountByType
@Type NVARCHAR(60),
@Count INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SELECT @Count = COUNT(*)
FROM dbo.Animals
WHERE [Type] = @Type;
END;
GO
-------------------------------------------------
-- Region: 6. Stored Procedure with XML Parameter
-------------------------------------------------
/*
6.1 AddAnimalFromXML: Parses XML input to insert an animal record.
Expected XML format:
<Animal>
<Name>...</Name>
<Type>...</Type>
<Age>...</Age>
</Animal>
*/
CREATE PROCEDURE AddAnimalFromXML
@AnimalData XML
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Name NVARCHAR(60), @Type NVARCHAR(60), @Age INT;
SET @Name = @AnimalData.value('(/Animal/Name)[1]', 'NVARCHAR(60)');
SET @Type = @AnimalData.value('(/Animal/Type)[1]', 'NVARCHAR(60)');
SET @Age = @AnimalData.value('(/Animal/Age)[1]', 'INT');
INSERT INTO dbo.Animals ([Name], [Type], [Age])
VALUES (@Name, @Type, @Age);
END;
GO
-------------------------------------------------
-- Region: 7. Stored Procedure with JSON Parameter
-------------------------------------------------
/*
7.1 AddAnimalFromJSON: Parses JSON input to insert an animal record.
Expected JSON format:
{"Name": "...", "Type": "...", "Age": ...}
*/
CREATE PROCEDURE AddAnimalFromJSON
@AnimalData NVARCHAR(MAX)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Name NVARCHAR(60), @Type NVARCHAR(60), @Age INT;
SET @Name = JSON_VALUE(@AnimalData, '$.Name');
SET @Type = JSON_VALUE(@AnimalData, '$.Type');
SET @Age = JSON_VALUE(@AnimalData, '$.Age');
INSERT INTO dbo.Animals ([Name], [Type], [Age])
VALUES (@Name, @Type, @Age);
END;
GO
-------------------------------------------------
-- Region: 8. Stored Procedure with Transactions and Error Handling
-------------------------------------------------
/*
8.1 TransferAnimal: Updates the type of an animal using transactions.
Rolls back if an error occurs or if no matching record is found.
*/
CREATE PROCEDURE TransferAnimal
@AnimalId INT,
@NewType NVARCHAR(60)
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRANSACTION;
-- Update the animal type
UPDATE dbo.Animals
SET [Type] = @NewType
WHERE Id = @AnimalId;
-- Check if any row was affected; if not, raise an error.
IF @@ROWCOUNT = 0
BEGIN
THROW 50000, 'No animal found with the provided Id.', 1;
END
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF XACT_STATE() <> 0
ROLLBACK TRANSACTION;
-- Return detailed error information
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() AS ErrorState,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
END;
GO
-------------------------------------------------
-- Region: End of Script
-------------------------------------------------