SQL Server error 1057 occurs when you attempt to use the IDENTITY function in a context where it is not permitted, most commonly within a SELECT statement that includes an ORDER BY clause or when trying to assign an identity value directly to a variable without a target table. This guide explains how to correctly generate sequential numbers using ROW_NUMBER() instead of IDENTITY(), as the latter is strictly reserved for SELECT INTO operations.

What You'll Need

  • Microsoft SQL Server Management Studio (SSMS) or Azure Data Studio
  • Administrative privileges on the database server
  • A valid connection to your SQL Server instance
  • Basic knowledge of T-SQL syntax

Step-by-Step

  1. Identify the problematic query: Locate the SQL statement causing error 1057. The error typically appears when you try to use IDENTITY(data_type, seed, increment) inside a standard SELECT list, especially if combined with ORDER BY. For example, this will fail: SELECT IDENTITY(int, 1, 1) AS RowNum FROM MyTable ORDER BY Name;
  2. Understand the limitation: Recognize that the IDENTITY function is designed specifically for creating new columns during a SELECT INTO operation. It cannot be used to add a row number to an existing result set in a simple SELECT query. This is a fundamental restriction of the SQL Server engine, not a configuration issue.
  3. Replace IDENTITY with ROW_NUMBER(): Modify your query to use the window function ROW_NUMBER(). This function generates a unique sequential integer for each row and works perfectly with ORDER BY. Change your query from using IDENTITY to: SELECT ROW_NUMBER() OVER (ORDER BY Name) AS RowNum, * FROM MyTable;
  4. Use SELECT INTO if creating a new table: If your goal is indeed to create a new table with an identity column, ensure you are using the correct syntax. You must use SELECT INTO and cannot include an ORDER BY clause in the same statement as the IDENTITY function. Example: SELECT IDENTITY(int, 1, 1) AS NewID, Name INTO #TempTable FROM MyTable;
  5. Execute the corrected query: Run the modified script in SSMS. Verify that no errors are returned and that the output contains the expected sequential numbers. If you used ROW_NUMBER(), check that the ordering matches your requirements.
  6. Test edge cases: Ensure the solution works with empty tables or large datasets. ROW_NUMBER() handles these scenarios gracefully, whereas improper use of IDENTITY might still throw errors if syntax rules are violated elsewhere in the code.

Common Mistakes

One frequent mistake is attempting to use IDENTITY to populate a variable or a temporary table via a direct assignment like SET @Var = IDENTITY(...), which is invalid syntax. Another common error is mixing IDENTITY with ORDER BY in a SELECT statement; even if you remove the ORDER BY, using IDENTITY outside of SELECT INTO will still trigger error 1057. Developers often confuse the need for a persistent identity column (which requires a table definition change) with the need for a transient row number (which only requires a query modification).

Tips

Always prefer ROW_NUMBER() for ad-hoc reporting and data retrieval tasks because it is more flexible and does not require creating intermediate tables. If you are migrating data and need to preserve specific identity values, consider using SET IDENTITY_INSERT ON instead of generating new ones with IDENTITY(). Note that menu labels and interface elements in SSMS may vary slightly depending on your version and regional settings, but the T-SQL syntax remains consistent across all modern versions of SQL Server.

Troubleshooting

If you continue to see error 1057 after applying the fixes above, double-check that you have not nested the IDENTITY function inside another aggregate function or expression, as this is also prohibited. Verify that your query is not part of a view or stored procedure that has cached execution plans; sometimes clearing the plan cache or restarting the service can help rule out stale metadata issues, though this is rare for syntax errors. If you are working in a restricted environment where you cannot modify the query structure, consult your database administrator to see if a different approach, such as using a cursor or a temporary table with an explicit identity column, is feasible. Always back up your data before performing structural changes to tables.

Frequently Asked Questions

Can I use IDENTITY in a CTE?

No, the IDENTITY function cannot be used in Common Table Expressions (CTEs). You should use ROW_NUMBER() within the CTE definition to generate sequential numbers.

Why does ORDER BY cause an error with IDENTITY?

The IDENTITY function is evaluated during the insertion of rows into a new table. Since ORDER BY sorts the final result set, SQL Server cannot guarantee the order of identity generation relative to the sort, leading to ambiguity and thus the error.

Is there a performance difference between ROW_NUMBER and IDENTITY?

For large datasets, ROW_NUMBER() can be resource-intensive due to sorting requirements. However, since IDENTITY cannot be used in the same contexts, ROW_NUMBER() is the standard and often optimized choice for ranking rows in queries.

How do I reset an identity column?

To reset an identity column, use the DBCC CHECKIDENT command. For example, DBCC CHECKIDENT ('TableName', RESEED, 0) resets the next identity value to 1. This is unrelated to error 1057 but useful for managing identity properties.