SQL Server ERROR_STATE: What It Means and How to Use It
In this article
ERROR_STATE is a small integer that SQL Server returns alongside an error number and message to identify which specific code path inside the engine raised the error. You cannot call it as a standalone function; you read it from the ERROR_STATE() function after a statement fails, or from the state column in sys.messages and the error output of TRY...CATCH. The shortest working approach is: run the failing statement inside a TRY...CATCH block, then select ERROR_NUMBER(), ERROR_STATE(), ERROR_MESSAGE() and the other ERROR_ functions in the CATCH block to see the exact state value. This guide explains what that number means, how to capture it reliably, and how to look up what each state represents.
What You'll Need
- Access to a SQL Server instance (any recent version) with permission to run queries and view system views.
- A client tool such as SQL Server Management Studio (SSMS) or Azure Data Studio, or any tool that can execute T-SQL.
- A test database where you can safely cause an error; avoid running experiments on production.
- Basic familiarity with TRY...CATCH and the ERROR_ functions.
Step-by-Step
- Understand what ERROR_STATE is. SQL Server errors have three core parts: a number (for example 547 for a foreign key violation), a severity, and a state. The state is a positive integer that the engine sets to distinguish different situations that share the same error number. For error 547, state 1 might mean a delete failed because of a reference, while another state might mean an insert or update failed. The state is not a general error code you can branch on across all errors; it is meaningful only in combination with the error number and the message text.
- Capture the state with TRY...CATCH. Write a batch that puts the risky statement inside a TRY block and reads the ERROR_ functions inside the CATCH block. For example:
BEGIN TRY SELECT 1/0; END TRY BEGIN CATCH SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_STATE() AS ErrorState, ERROR_SEVERITY() AS Severity, ERROR_MESSAGE() AS Message; END CATCH;Run it and note the state value returned. The same pattern works for any statement that might fail. - Read the state from the error message itself. When a statement fails outside a CATCH block, the client often shows a message like "Msg 547, Level 16, State 0, Line 1". That State number is the same value ERROR_STATE() would return. If your tool hides it, check the Messages tab in SSMS or the error object returned by your driver.
- Look up the documented states. Query
sys.messagesto see the messages and states registered for an error number:SELECT message_id, language_id, severity, is_event_logged, text FROM sys.messages WHERE message_id = 547;The text often includes a placeholder such as "%S_STATE%" or a description that explains the state. For system errors, the state is usually documented in the message text or in the SQL Server documentation for that error number. - Use the state in application error handling. When your code catches an exception, log the error number, state, severity, procedure name and line number together. Branch on the error number first, then use the state to refine the message you show the user. For example, a foreign key error with one state might mean the parent row is missing, while another state means the child row still exists. Do not hard-code state values without confirming them against your SQL Server version, because the engine can add new states over time.
- Test the same error on your version. Because state values are internal, the safest way to know what a state means on your server is to reproduce the error deliberately in a test database and record the state, message and severity. Keep a small table of the errors your application actually encounters, with the number, state and the action your code takes.
- Check related ERROR_ functions. Inside a CATCH block you can also read ERROR_SEVERITY(), ERROR_PROCEDURE(), ERROR_LINE() and ERROR_MESSAGE(). Outside a CATCH block, these functions return NULL, so always capture them in the CATCH block or in the client's error handler.
Common Mistakes
- Calling ERROR_STATE() outside a CATCH block and expecting a value. It returns NULL unless it is executed in the scope of a CATCH block.
- Treating the state as a universal error code. Two different error numbers can share the same state value, and the same error number can have several states.
- Assuming the state is stable across versions. States are internal identifiers; a service pack or upgrade can change which state is raised for a given condition.
- Ignoring severity. A state alone does not tell you whether the error is informational, a constraint violation or something that terminates the batch.
- Logging only the message text. Without the number and state, you lose the ability to group and search errors reliably.
Tips
- Log ERROR_NUMBER(), ERROR_STATE(), ERROR_SEVERITY(), ERROR_PROCEDURE() and ERROR_LINE() together in a single row so you can reconstruct the failure later.
- Use THROW instead of RAISERROR in new code; THROW preserves the original error number and state when re-raising inside a CATCH block.
- Keep a reference table of the errors your application handles, with the number and state you observed on your test server.
- When you re-raise an error, include the original state so downstream logging still shows the root cause.
- Use a dedicated test database for experiments so a deliberate constraint violation does not affect real data.
Troubleshooting
If ERROR_STATE() returns NULL, confirm the call is inside a CATCH block and that the statement actually raised an error. If the CATCH block runs but the state looks unexpected, check whether a trigger or nested procedure raised the error; ERROR_PROCEDURE() and ERROR_LINE() will show where it came from. If your client does not display the state, run the failing statement inside a TRY...CATCH block and select the ERROR_ functions explicitly. If you cannot find documentation for a state, reproduce the error in a test database and compare the message text; the combination of number, state and message is the reliable identifier. If an upgrade changed the state you were branching on, stop hard-coding that value and branch on the error number and message instead.
Frequently Asked Questions
Is ERROR_STATE a function I can call anywhere?
It is a built-in function, but it only returns a meaningful value inside a CATCH block. Outside that scope it returns NULL.
What is the difference between error number and error state?
The number identifies the kind of error, such as a foreign key violation. The state identifies which specific condition inside that error was raised. Both are needed to understand the failure.
Can I rely on a specific state value in my application code?
Only after testing it on your SQL Server version. States are internal and can change between versions, so branch on the error number first and use the state as additional context.
Where can I see the states defined for a system error?
Query sys.messages for the message_id and read the text, and check the SQL Server documentation for that error number. The message text often describes what each state means.
Does ERROR_STATE() work with THROW?
Yes. When you use THROW inside a CATCH block without arguments, it re-raises the original error, preserving the number and state. You can also specify a state when raising a custom error.
Why does the same error show different states on different servers?
Different SQL Server versions, service packs or code paths can raise the same error number with different states. Compare the message text and the operation that failed, not just the state value.