-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTryCatch.sql
More file actions
55 lines (44 loc) · 1.37 KB
/
Copy pathTryCatch.sql
File metadata and controls
55 lines (44 loc) · 1.37 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
IF OBJECT_ID('spAverageBalance','P') IS NOT NULL
DROP PROC spAverageBalance
GO
CREATE PROC spAverageBalance
@PersonFrom int,
@PersonTo int,
@AvgBalance money OUT
AS
BEGIN
SET NOCOUNT ON
--IF EXISTS (SELECT 1 FROM Person WHERE PersonID IN (@PersonFrom,@PersonTo))
BEGIN TRY
BEGIN
DECLARE @TotalSum smallmoney
DECLARE @PersonCount int
SELECT @PersonCount = COUNT(DISTINCT P.PersonID) FROM Person P
WHERE P.PersonID BETWEEN @PersonFrom AND @PersonTo
SELECT @TotalSum = ISNULL(SUM(Amount),0) FROM Transactions
WHERE PersonID BETWEEN @PersonFrom AND @PersonTo
SET @AvgBalance = @TotalSum/@PersonCount
END
END TRY
BEGIN CATCH
set @AvgBalance = 0
if ERROR_NUMBER() = 8134 -- 0 Divide Error
BEGIN
RETURN 8134
END
ELSE
THROW 58994,'This is a medium Error',1
--THROW Parameters (Msg Number,Severity,State,Procedure,Line,Message)
---Gets the System Error Information
--SELECT ERROR_MESSAGE() AS ErrorMsg
-- ,ERROR_LINE() AS ErrorLine
-- ,ERROR_NUMBER() AS ErrorNumber
-- ,ERROR_SEVERITY() AS ErrorSeverity
-- ,ERROR_PROCEDURE() AS ErrorProcedure
-- ,ERROR_STATE() AS ErrorState
END CATCH
END
GO
DECLARE @AvgBalance money,@ReturnStatus int
EXEC @ReturnStatus = spAverageBalance 5,34,@AvgBalance OUT
SELECT @AvgBalance as AvgBalance,@ReturnStatus AS ReturnStatus