SQL Flow Field Guide

ERROR_NUMBER

Catalog support for SQL Server 2022 (16.x) and 2025 (17.x)

reads error state

What changes

Input
the error that activated the current CATCH block
Output or change
error attribute (NULL outside CATCH)
Return type
int

Syntax at a glance

ERROR_NUMBER ( )

Work example

Branch on a known SQL Server error number inside a CATCH block.

Handle only errors with a documented recovery path and rethrow unexpected failures.

Check your work ERROR_NUMBER returns null outside CATCH and nested handlers can report different error scopes.

Practice SQL

Open in SQL Flow
BEGIN TRY
    SELECT 1 / 0;
END TRY
BEGIN CATCH
    SELECT ERROR_NUMBER() AS error_number;
END CATCH;

Open Microsoft Learn for full syntax, permissions, and details for your version.