- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLQueryErrors.sql
More file actions
Latest commit
165 lines (133 loc) · 5.08 KB
/
Copy pathSQLQueryErrors.sql
File metadata and controls
165 lines (133 loc) · 5.08 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
-- Error Handeling
--An error indicates a problem or notable issue that arises during a database operation.
--Errors can be generated by the SQL Server Database Engine in response to an event or failure at the system
--level; or you can generate application errors in your Transact-SQL code.
use AdventureWorksDW2020;
-- System errors are predefined, and you can view them in the sys.messages system view.
-- You can generate errors in Transact-SQL code to respond to application-specific conditions or to
-- customize information sent to client applications in response to system errors.
-- In addition, you can define custom error messages, members of the sysadmin server role can also use an
-- additional parameter, @with_log. When set to TRUE, the error will also be recorded in the Windows
-- Application log.
-- Messages can be replaced without deleting them first by using the @replace = ‘replace’ option.
RAISERROR (N'%s %d', -- Message text,
10, -- Severity,
1, -- State,
N'Custom error message number',
2)
--Errors raised by THROW are always severity 16.
THROW50001, 'An Error Occured',0
-- @@ERROR is a system variable that holds the error number of the last error that has occurred.
RAISERROR(N'Message', 16, 1);
IF@@ERROR<>0
PRINT'Error='+CAST(@@ERRORASVARCHAR(8));
GO
-- Alerts can be created for specific error messages. The alerting service works by registering itself
-- as a callback service with the event logging service.
-- Try Catch Blocks
-- If the THROW statement is used in a CATCH block without any parameters, it will rethrow the error that
-- caused the code to enter the CATCH block.
BEGINTRY
-- code to be executed
ENDTRY
BEGINCATCH
PRINT ERROR_MESSAGE0.
THROW
ENDCATCH
-- exercises:
SELECTCAST(N'Some text'ASint);
BEGINTRY
SELECTCAST(N'Some text'ASint);END TRYBEGIN CATCHPRINT'Error';
ENDCATCH;DECLARE @num varchar(20) ='0';
BEGINTRY
PRINT5. /CAST(@num ASnumeric(10,4));
ENDTRY
BEGINCATCH
ENDCATCH;
DECLARE @num1 varchar(20) ='0';
BEGINTRY
PRINT5. /CAST(@num1 ASnumeric(10,4));
ENDTRY
BEGINCATCHPRINT'Error Number: '+CAST(ERROR_NUMBER() ASvarchar(10));
PRINT'Error Message: '+ERROR_MESSAGE();
ENDCATCH;
DECLARE @num2 varchar(20) ='A';
DECLARE @num3 varchar(20) =' 1000000000';
DECLARE @num4 varchar(20) ='A';BEGINTRY
PRINT5. /CAST(@num4 ASnumeric(10,4));
ENDTRY
BEGINCATCH
IFERROR_NUMBER() IN (245, 8114)
BEGIN
PRINT'Handling conversion error...'
END
ELSE
BEGIN
PRINT'Handling non-conversion error...';
END;
PRINT'Error Number: '+CAST(ERROR_NUMBER() ASvarchar(10));
PRINT'Error Message: '+ERROR_MESSAGE();ENDCATCH;
go
create proceduresCREATE PROCEDUREdbo.GetErrorInfoAS
PRINT'Error Number: '+CAST(ERROR_NUMBER() ASvarchar(10));
PRINT'Error Message: '+ERROR_MESSAGE();PRINT'Error Severity: '+CAST(ERROR_SEVERITY() ASvarchar(10));
PRINT'Error State: '+CAST(ERROR_STATE() ASvarchar(10));
PRINT'Error Line: '+CAST(ERROR_LINE() ASvarchar(10));
PRINT'Error Proc: '+COALESCE(ERROR_PROCEDURE(), 'Not within procedure');
GOexec dbo.GetErrorInfogoDECLARE @num varchar(20) ='0';
BEGINTRY
PRINT5. /CAST(@num ASnumeric(10,4));
ENDTRY
BEGIN
CATCH
EXECUTEdbo.GetErrorInfo;ENDCATCH;
go
--Rethrow the Existing Error Back to a Client
DECLARE @num varchar(20) ='0';
BEGINTRY
PRINT5. /CAST(@num ASnumeric(10,4));
END TRYBEGIN
CATCH
EXECUTEdbo.GetErrorInfo; THROW;
ENDCATCH;
GODECLARE @num varchar(20) ='A';
BEGINTRY
PRINT5. /CAST(@num ASnumeric(10,4));
END TRYBEGIN
CATCH
EXECUTEdbo.GetErrorInfo;
IFERROR_NUMBER() =8134BEGIN
PRINT'Handling devision by zero...';
END
ELSE
BEGIN
PRINT'Throwing original error';
THROW;
END;
ENDCATCH;
goDECLARE @customerID ASINT=30110;
DECLARE @fname ASNVARCHAR(20);
DECLARE @lname ASNVARCHAR(30);
DECLARE @maxReturns ASINT=1;
WHILE @maxReturns <=10
BEGIN
try
SELECT @fname = FirstName, @lname = LastName FROMSalesLT.CustomerWHERE CustomerID = @CustomerID;
if@@ROWCOUNT>0
BEGIN
PRINTCAST(@customerID asNVARCHAR(20)) +N' '+ @fname +N' '+ @lname;
endtry
BEGINCATCH
PRINT'Unable to run query';
endCATCH
SET @maxReturns +=1; SET @CustomerID +=1; END;GODECLARE @num10 varchar(20) ='Challenge 2';
BEGINTRYPRINT'Casting: '+CAST(@num10 ASnumeric(10,4));END TRYBEGIN CATCH
PRINT'Error Message: '+ERROR_MESSAGE();
ENDCATCH;
go
CREATEPROCEDUREdbo.DisplayErrorDetailsAS
PRINT'ERROR INFORMATION';PRINT'Error Number: '+CAST(ERROR_NUMBER() ASvarchar(10));
PRINT'Error Message: '+ERROR_MESSAGE();PRINT'Error Severity: '+CAST(ERROR_SEVERITY() ASvarchar(10));GO
DECLARE @num11 varchar(20) ='Challenge 2';BEGINTRYPRINT'Casting: '+CAST(@num11 ASnumeric(10,4));
ENDTRYBEGINCATCHEXECUTEdbo.DisplayErrorDetails;
ENDCATCH;