You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This function returns the name of the stored procedure or trigger where an error occurs, if that error caused the CATCH block of a TRY...CATCH construct to execute.
29
-
- SQL Server 2017 thru [current version](../../sql-server/what-s-new-in-sql-server-2019.md) returns `schema_name.stored_procedure_name`
When called in a CATCH block, `ERROR_PROCEDURE` returns the name of the stored procedure or trigger in which the error originated.
45
-
46
-
`ERROR_PROCEDURE` returns NULL if the error did not occur within a stored procedure or trigger.
47
-
48
-
`ERROR_PROCEDURE` returns NULL when called outside the scope of a CATCH block.
49
-
50
-
## Remarks
51
-
`ERROR_PROCEDURE` supports calls anywhere within the scope of a CATCH block.
52
-
53
-
`ERROR_PROCEDURE` returns the name of the stored procedure or trigger where an error occurs, regardless of how many times it runs, or where it runs, within the scope of the `CATCH` block. This contrasts with a function like @@ERROR, which only returns an error number in the statement immediately following the one that causes an error.
54
-
55
-
## Examples: [!INCLUDE[ssazuresynapse-md](../../includes/ssazuresynapse-md.md)] and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
56
-
57
-
### A. Using ERROR_PROCEDURE in a CATCH block
58
-
This example shows a stored procedure that generates a divide-by-zero error. `ERROR_PROCEDURE` returns the name of the stored procedure where the error occurred.
59
-
60
-
```sql
61
-
-- Verify that the stored procedure does not already exist.
62
-
IF OBJECT_ID ( 'usp_ExampleProc', 'P' ) IS NOT NULL
63
-
DROP PROCEDURE usp_ExampleProc;
64
-
GO
65
-
66
-
-- Create a stored procedure that
67
-
-- generates a divide-by-zero error.
68
-
CREATE PROCEDURE usp_ExampleProc
69
-
AS
70
-
SELECT1/0;
71
-
GO
72
-
73
-
BEGIN TRY
74
-
-- Execute the stored procedure inside the TRY block.
This function returns the name of the stored procedure or trigger where an error occurs, if that error caused the `CATCH` block of a `TRY...CATCH` construct to execute.
31
+
32
+
-[!INCLUDE [sssql17-md](../../includes/sssql17-md.md)] and later versions return `schema_name.stored_procedure_name`
33
+
-[!INCLUDE [sssql16-md](../../includes/sssql16-md.md)] and [!INCLUDE [ssazure-sqldb](../../includes/ssazure-sqldb.md)] return `stored_procedure_name`
When called in a `CATCH` block, `ERROR_PROCEDURE` returns the name of the stored procedure or trigger in which the error originated.
50
+
51
+
`ERROR_PROCEDURE` returns `NULL` if the error didn't occur within a stored procedure or trigger.
52
+
53
+
`ERROR_PROCEDURE` returns `NULL` when called outside the scope of a `CATCH` block.
54
+
55
+
## Remarks
56
+
57
+
`ERROR_PROCEDURE` supports calls anywhere within the scope of a `CATCH` block.
58
+
59
+
`ERROR_PROCEDURE` returns the name of the stored procedure or trigger where an error occurs, regardless of how many times it runs, or where it runs, within the scope of the `CATCH` block. This result contrasts with a function like `@@ERROR`, which only returns an error number in the statement immediately following the one that causes an error.
60
+
61
+
## Examples: Azure Synapse Analytics and Analytics Platform System (PDW)
62
+
63
+
### A. Use ERROR_PROCEDURE in a CATCH block
64
+
65
+
This example shows a stored procedure that generates a divide-by-zero error. `ERROR_PROCEDURE` returns the name of the stored procedure where the error occurred.
66
+
67
+
```sql
68
+
-- Verify that the stored procedure does not already exist.
69
+
IF OBJECT_ID('usp_ExampleProc', 'P') IS NOT NULL
70
+
DROP PROCEDURE usp_ExampleProc;
71
+
GO
72
+
73
+
-- Create a stored procedure that
74
+
-- generates a divide-by-zero error.
75
+
CREATE PROCEDURE usp_ExampleProc
76
+
AS
77
+
SELECT1/0;
78
+
GO
79
+
80
+
BEGIN TRY
81
+
-- Execute the stored procedure inside the TRY block.
### B. Use ERROR_PROCEDURE in a CATCH block with other error-handling tools
105
+
106
+
This example shows a stored procedure that generates a divide-by-zero error. Along with the name of the stored procedure where the error occurred, the stored procedure returns information about the error.
95
107
96
-
97
-
### B. Using ERROR_PROCEDURE in a CATCH block with other error-handling tools
98
-
This example shows a stored procedure that generates a divide-by-zero error. Along with the name of the stored procedure where the error occurred, the stored procedure returns information about the error.
99
-
100
108
```sql
101
-
-- Verify that the stored procedure does not already exist.
102
-
IF OBJECT_ID ( 'usp_ExampleProc', 'P' ) IS NOT NULL
103
-
DROP PROCEDURE usp_ExampleProc;
104
-
GO
105
-
106
-
-- Create a stored procedure that
107
-
-- generates a divide-by-zero error.
108
-
CREATE PROCEDURE usp_ExampleProc
109
-
AS
110
-
SELECT1/0;
111
-
GO
112
-
113
-
BEGIN TRY
114
-
-- Execute the stored procedure inside the TRY block.
115
-
EXECUTE usp_ExampleProc;
116
-
END TRY
117
-
BEGIN CATCH
118
-
SELECT
119
-
ERROR_NUMBER() AS ErrorNumber,
120
-
ERROR_SEVERITY() AS ErrorSeverity,
121
-
ERROR_STATE() AS ErrorState,
122
-
ERROR_PROCEDURE() AS ErrorProcedure,
123
-
ERROR_MESSAGE() AS ErrorMessage,
124
-
ERROR_LINE() AS ErrorLine;
125
-
END CATCH;
109
+
-- Verify that the stored procedure does not already exist.
0 commit comments