Skip to content

Commit 9be6823

Browse files
authored
Refresh ERROR_PROCEDURE (UUF 433810) (#34333)
1 parent f159902 commit 9be6823

File tree

1 file changed

+115
-109
lines changed

1 file changed

+115
-109
lines changed
Lines changed: 115 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
---
22
title: "ERROR_PROCEDURE (Transact-SQL)"
3-
description: "ERROR_PROCEDURE (Transact-SQL)"
3+
description: ERROR_PROCEDURE returns the name of the stored procedure or trigger where an error occurs.
44
author: markingmyname
55
ms.author: maghan
6-
ms.date: "03/16/2017"
6+
ms.reviewer: randolphwest
7+
ms.date: 06/05/2025
78
ms.service: sql
89
ms.subservice: t-sql
910
ms.topic: reference
@@ -20,68 +21,75 @@ helpviewer_keywords:
2021
- "errors [SQL Server], trigger where occurred"
2122
dev_langs:
2223
- "TSQL"
23-
monikerRange: ">= aps-pdw-2016 || = azuresqldb-current || = azure-sqldw-latest || >= sql-server-2016 || >= sql-server-linux-2017 || = azuresqldb-mi-current||=fabric"
24+
monikerRange: ">=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric"
2425
---
2526
# ERROR_PROCEDURE (Transact-SQL)
26-
[!INCLUDE [sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw.md)]
27-
28-
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`
30-
- SQL Server 2016 returns `stored_procedure_name`
31-
32-
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: [Transact-SQL syntax conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
33-
34-
## Syntax
35-
36-
```syntaxsql
37-
ERROR_PROCEDURE ( )
38-
```
39-
40-
## Return Types
41-
**nvarchar(128)**
42-
43-
## Return Value
44-
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-
SELECT 1/0;
71-
GO
72-
73-
BEGIN TRY
74-
-- Execute the stored procedure inside the TRY block.
75-
EXECUTE usp_ExampleProc;
76-
END TRY
77-
BEGIN CATCH
78-
SELECT ERROR_PROCEDURE() AS ErrorProcedure;
79-
END CATCH;
80-
GO
81-
```
82-
83-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
84-
```
27+
28+
[!INCLUDE [sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw.md)]
29+
30+
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`
34+
35+
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: [Transact-SQL syntax conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
36+
37+
## Syntax
38+
39+
```syntaxsql
40+
ERROR_PROCEDURE ( )
41+
```
42+
43+
## Return types
44+
45+
**nvarchar(128)**
46+
47+
## Return value
48+
49+
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+
SELECT 1 / 0;
78+
GO
79+
80+
BEGIN TRY
81+
-- Execute the stored procedure inside the TRY block.
82+
EXECUTE usp_ExampleProc;
83+
END TRY
84+
BEGIN CATCH
85+
SELECT ERROR_PROCEDURE() AS ErrorProcedure;
86+
END CATCH;
87+
GO
88+
```
89+
90+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
91+
92+
```output
8593
-----------
8694
8795
(0 row(s) affected)
@@ -91,43 +99,43 @@ ErrorProcedure
9199
usp_ExampleProc
92100
93101
(1 row(s) affected)
94-
```
102+
```
103+
104+
### 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.
95107

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-
100108
```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-
SELECT 1/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.
110+
IF OBJECT_ID('usp_ExampleProc', 'P') IS NOT NULL
111+
DROP PROCEDURE usp_ExampleProc;
112+
GO
113+
114+
-- Create a stored procedure that
115+
-- generates a divide-by-zero error.
116+
CREATE PROCEDURE usp_ExampleProc
117+
AS
118+
SELECT 1 / 0;
126119
GO
127-
```
128120

129-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
130-
```
121+
BEGIN TRY
122+
-- Execute the stored procedure inside the TRY block.
123+
EXECUTE usp_ExampleProc;
124+
END TRY
125+
BEGIN CATCH
126+
SELECT ERROR_NUMBER() AS ErrorNumber,
127+
ERROR_SEVERITY() AS ErrorSeverity,
128+
ERROR_STATE() AS ErrorState,
129+
ERROR_PROCEDURE() AS ErrorProcedure,
130+
ERROR_MESSAGE() AS ErrorMessage,
131+
ERROR_LINE() AS ErrorLine;
132+
END CATCH;
133+
GO
134+
```
135+
136+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
137+
138+
```output
131139
-----------
132140
133141
(0 row(s) affected)
@@ -137,18 +145,16 @@ ErrorNumber ErrorSeverity ErrorState ErrorProcedure ErrorMessage
137145
8134 16 1 usp_ExampleProc Divide by zero error encountered. 6
138146
139147
(1 row(s) affected)
148+
```
149+
150+
## Related content
140151

141-
```
142-
143-
144-
## See Also
145-
[sys.messages (Transact-SQL)](../../relational-databases/system-catalog-views/messages-for-errors-catalog-views-sys-messages.md)
146-
[TRY...CATCH (Transact-SQL)](../../t-sql/language-elements/try-catch-transact-sql.md)
147-
[ERROR_LINE (Transact-SQL)](../../t-sql/functions/error-line-transact-sql.md)
148-
[ERROR_MESSAGE (Transact-SQL)](../../t-sql/functions/error-message-transact-sql.md)
149-
[ERROR_NUMBER (Transact-SQL)](../../t-sql/functions/error-number-transact-sql.md)
150-
[ERROR_SEVERITY (Transact-SQL)](../../t-sql/functions/error-severity-transact-sql.md)
151-
[ERROR_STATE (Transact-SQL)](../../t-sql/functions/error-state-transact-sql.md)
152-
[RAISERROR (Transact-SQL)](../../t-sql/language-elements/raiserror-transact-sql.md)
153-
[@@ERROR (Transact-SQL)](../../t-sql/functions/error-transact-sql.md)
154-
152+
- [sys.messages](../../relational-databases/system-catalog-views/messages-for-errors-catalog-views-sys-messages.md)
153+
- [TRY...CATCH (Transact-SQL)](../language-elements/try-catch-transact-sql.md)
154+
- [ERROR_LINE (Transact-SQL)](error-line-transact-sql.md)
155+
- [ERROR_MESSAGE (Transact-SQL)](error-message-transact-sql.md)
156+
- [ERROR_NUMBER (Transact-SQL)](error-number-transact-sql.md)
157+
- [ERROR_SEVERITY (Transact-SQL)](error-severity-transact-sql.md)
158+
- [ERROR_STATE (Transact-SQL)](error-state-transact-sql.md)
159+
- [RAISERROR (Transact-SQL)](../language-elements/raiserror-transact-sql.md)
160+
- [@@ERROR (Transact-SQL)](error-transact-sql.md)

0 commit comments

Comments
 (0)