3

ERROR_SEVERITY (Transact-SQL)

 7 months ago
source link: https://learn.microsoft.com/en-us/sql/t-sql/functions/error-severity-transact-sql?view=sql-server-ver16
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

ERROR_SEVERITY (Transact-SQL)

Skip to main content

The ultimate SQL, Azure Databases, Fabric & Azure AI learning event: Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register now
Version
Search
  • SQL Server
  • Docs navigation tips
  • Previous versions 2005-2014
  • Overview
  • Business continuity
  • Database design
  • Development
  • Internals & Architecture
  • Installation
  • Migrate & load data
  • Manage, monitor, & tune
  • Query data
  • Reporting & Analytics
  • Security
  • Tools
  • Tutorials
  • SQL Server on Linux
  • SQL on Azure
  • Azure Arc
  • Resources
  • Reference
    • Azure Data CLI azcli
    • Database samples
    • Errors & events
    • Event classes
    • Native interfaces
    • SQL PowerShell
    • System catalog views
    • System compatibility views
    • System dynamic management views
    • System functions
    • System information schema views
    • System stored procedures
    • System tables
    • Transact-SQL (T-SQL) Reference
      • Transact-SQL (T-SQL) Reference
      • Date & time
      • hierarchyid methods (database engine)
      • Numeric
      • String & binary
      • Spatial geography & instances (geography Data Type)
      • Spatial geometry & instances (geometry Data Type)
      • Data types
      • Functions
      • Language elements
      • Queries
      • Statements
    • xQuery

ERROR_SEVERITY (Transact-SQL)

  • Article
  • 05/23/2023

In this article

Applies to: yes-icon.svg?view=sql-server-ver16SQL Server yes-icon.svg?view=sql-server-ver16Azure SQL Database yes-icon.svg?view=sql-server-ver16Azure SQL Managed Instance yes-icon.svg?view=sql-server-ver16Azure Synapse Analytics yes-icon.svg?view=sql-server-ver16Analytics Platform System (PDW) yes-icon.svg?view=sql-server-ver16SQL analytics endpoint in Microsoft Fabric yes-icon.svg?view=sql-server-ver16Warehouse in Microsoft Fabric

This function returns the severity value of the error where an error occurs, if that error caused the CATCH block of a TRY...CATCH construct to execute.

topic-link-icon.svg?view=sql-server-ver16Transact-SQL syntax conventions

Syntax

syntaxsql
ERROR_SEVERITY ( )  

To view Transact-SQL syntax for SQL Server 2014 (12.x) and earlier versions, see Previous versions documentation.

Return Types

int

Return Value

When called in a CATCH block where an error occurs, ERROR_SEVERITY returns the severity value of the error that caused the CATCH block to run.

ERROR_SEVERITY returns NULL if called outside the scope of a CATCH block.

Remarks

ERROR_SEVERITY supports calls anywhere within the scope of a CATCH block..

ERROR_SEVERITY returns the error severity value of an error, 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.

ERROR_SEVERITY typically operates in a nested CATCH block. ERROR_SEVERITY returns the error severity value specific to the scope of the CATCH block that referenced that CATCH block. For example, the CATCH block of an outer TRY...CATCH construct could have an inner TRY...CATCH construct. Inside that inner CATCH block, ERROR_SEVERITY returns the severity value of the error that invoked the inner CATCH block. If ERROR_SEVERITY runs in the outer CATCH block, it returns the error severity value of the error that invoked that outer CATCH block.

Examples: Azure Synapse Analytics and Analytics Platform System (PDW)

A. Using ERROR_SEVERITY in a CATCH block

This example shows a stored procedure that generates a divide-by-zero error. ERROR_SEVERITY returns the severity value of that error.

BEGIN TRY  
    -- Generate a divide-by-zero error.  
    SELECT 1/0;  
END TRY  
BEGIN CATCH  
    SELECT ERROR_SEVERITY() AS ErrorSeverity;  
END CATCH;  
GO  

Here is the result set.

-----------

(0 row(s) affected)

ErrorSeverity
-------------
16

(1 row(s) affected)

B. Using ERROR_SEVERITY in a CATCH block with other error-handling tools

This example shows a SELECT statement that generates a divide by zero error. The stored procedure returns information about the error.

BEGIN TRY  
    -- Generate a divide-by-zero error.  
    SELECT 1/0;  
END TRY  
BEGIN CATCH  
    SELECT  
        ERROR_NUMBER() AS ErrorNumber,  
        ERROR_SEVERITY() AS ErrorSeverity,  
        ERROR_STATE() AS ErrorState,  
        ERROR_PROCEDURE() AS ErrorProcedure,  
        ERROR_LINE() AS ErrorLine,  
        ERROR_MESSAGE() AS ErrorMessage;  
END CATCH;  
GO  

Here is the result set.

-----------

(0 row(s) affected)

ErrorNumber ErrorSeverity ErrorState  ErrorProcedure  ErrorLine   ErrorMessage
----------- ------------- ----------- --------------- ----------- ----------------------------------
8134        16            1           NULL            4           Divide by zero error encountered.

(1 row(s) affected)

See Also

sys.messages (Transact-SQL)
TRY...CATCH (Transact-SQL)
ERROR_LINE (Transact-SQL)
ERROR_MESSAGE (Transact-SQL)
ERROR_NUMBER (Transact-SQL)
ERROR_PROCEDURE (Transact-SQL)
ERROR_STATE (Transact-SQL)
RAISERROR (Transact-SQL)
@@ERROR (Transact-SQL)
Errors and Events Reference (Database Engine)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK