ERROR_MESSAGE
reads error state
What changes
- Input
- the error that activated the current CATCH block
- Output or change
- error attribute (NULL outside CATCH)
- Return type
- nvarchar(4000)
Syntax at a glance
ERROR_MESSAGE ( )
Work example
Read the caught error text inside a CATCH block for controlled diagnostics.
Log structured error details in the handler and avoid returning internal details to an untrusted caller.
Check your work ERROR_MESSAGE returns null outside the scope of a CATCH block.
Practice SQL
Open in SQL FlowBEGIN TRY
SELECT 1 / 0;
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE() AS error_text;
END CATCH;
Open Microsoft Learn for full syntax, permissions, and details for your version.