Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SQL Server IDENT_CURRENT Function: How to Use It

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

IDENT_CURRENT returns the last identity value generated for a specified table in any session and any scope. You call it like a function: SELECT IDENT_CURRENT('YourTable');. It is useful when you need to know the most recent identity value assigned to a table, regardless of who inserted it or whether the insert happened in the same connection. Before using it, you need a SQL Server database with at least one table that has an identity column, and you need permission to query that table (typically SELECT permission or membership in a role that has it).

What You'll Need

  • Access to a SQL Server instance (any recent version supports IDENT_CURRENT).
  • A database containing a table with an identity column. If you don't have one, you can create a test table (see Step 1).
  • A query tool such as SQL Server Management Studio (SSMS), Azure Data Studio, or any client that can run T-SQL.
  • Permission to run SELECT against the table, or membership in a role like db_datareader.

Step-by-Step

  1. Connect to your database. Open SSMS or Azure Data Studio and connect to the SQL Server instance. In the Object Explorer, expand Databases and select the database you want to work with. You can also run USE YourDatabaseName; at the top of your query window. Menu labels can differ slightly by version, but the connection and database selection steps are consistent.
  2. Identify a table with an identity column. Run a query against system views to find one, for example:
    SELECT OBJECT_NAME(object_id) AS TableName, name AS ColumnName FROM sys.identity_columns;
    This lists every identity column in the current database. Pick a table name to use in the next steps. If no rows return, create a test table:
    CREATE TABLE dbo.TestIdent (ID INT IDENTITY(1,1) PRIMARY KEY, Name NVARCHAR(50));
  3. Insert a row to generate an identity value. If the table already has data, you can skip this, but to see IDENT_CURRENT in action, insert a row:
    INSERT INTO dbo.TestIdent (Name) VALUES ('First row');
    The identity column will receive a value automatically. You do not specify it in the INSERT.
  4. Call IDENT_CURRENT with the table name. Run:
    SELECT IDENT_CURRENT('dbo.TestIdent') AS LastIdentity;
    The result is the last identity value generated for that table. Note that the argument is a string and must include the schema if the table is not in the default schema (e.g., 'dbo.TestIdent'). If you omit the schema, SQL Server assumes the default schema for the current user, which may not be dbo.
  5. Compare with SCOPE_IDENTITY and @@IDENTITY (optional). To understand the difference, run these in the same batch after an insert:
    SELECT SCOPE_IDENTITY() AS SameScope, @@IDENTITY AS AnyScope, IDENT_CURRENT('dbo.TestIdent') AS AnySession;
    SCOPE_IDENTITY returns the last identity inserted in the current scope (stored procedure, trigger, or batch). @@IDENTITY returns the last identity inserted in the current session, regardless of scope. IDENT_CURRENT returns the last identity inserted in any session. This matters when triggers or other sessions are inserting rows.
  6. Use IDENT_CURRENT in a stored procedure or script. You can assign the result to a variable:
    DECLARE @LastID INT; SET @LastID = IDENT_CURRENT('dbo.TestIdent'); SELECT @LastID;
    This is handy when you need the value for logging or further processing. Remember that IDENT_CURRENT is not connection-safe: another session could insert a row between your insert and your call, so the value may not be the one your insert generated.
  7. Check the seed and increment (optional). If you need to know the next value, you can use IDENT_INCR and IDENT_SEED:
    SELECT IDENT_SEED('dbo.TestIdent') AS Seed, IDENT_INCR('dbo.TestIdent') AS Increment;
    These return the original seed and increment specified when the table was created. They are metadata functions and do not require data to be present.

Common Mistakes

  • Forgetting the schema. If the table is not in the default schema (often dbo), IDENT_CURRENT('TableName') may return NULL. Always include the schema, e.g., 'dbo.Orders'.
  • Assuming IDENT_CURRENT is session-safe. It returns the last value for the table across all sessions. If another user inserts a row after you do, IDENT_CURRENT will show their value, not yours. Use SCOPE_IDENTITY() when you need the value from your own insert.
  • Using it on a table without an identity column. IDENT_CURRENT returns NULL if the table has no identity column. Check sys.identity_columns first.
  • Passing a variable without quotes. The argument must be a string literal or a variable of a character type. IDENT_CURRENT(@TableName) works if @TableName is NVARCHAR, but IDENT_CURRENT(TableName) without quotes will cause an error.
  • Expecting it to reflect uncommitted inserts from other transactions. IDENT_CURRENT reads the last value generated, even if the transaction that generated it is still open. This can lead to reading a value that later rolls back, so treat it as a hint, not a guarantee.

Tips

  • Use IDENT_CURRENT when you need the last identity value for a table regardless of session, such as for auditing or reporting.
  • For getting the identity value of the row you just inserted, prefer SCOPE_IDENTITY() or the OUTPUT clause. The OUTPUT clause is the most reliable and works with multi-row inserts.
  • IDENT_CURRENT is not affected by scope or session, so it is useful in triggers when you need to know the last value for a table, but be aware of concurrency.
  • If you need the next identity value without inserting, you can use DBCC CHECKIDENT with NORESEED to see the current value, but that is a different tool and has its own caveats.
  • Always schema-qualify table names in these functions to avoid NULL results.

Troubleshooting

  • IDENT_CURRENT returns NULL. Check that the table exists, that you have spelled the name correctly, and that the schema is included. Also verify the table has an identity column by querying sys.identity_columns.
  • Permission denied. You need SELECT permission on the table. If you are not the owner, ask an administrator to grant SELECT or add you to a role like db_datareader.
  • Unexpected value after inserts. Remember that IDENT_CURRENT is not session-specific. If other sessions are inserting, the value may not match your last insert. Use SCOPE_IDENTITY() for your own session's last insert.
  • Error: Invalid object name. The table may not exist in the current database, or you may be connected to the wrong database. Run SELECT DB_NAME(); to confirm, and use a fully qualified name like YourDB.dbo.YourTable if needed.
  • Function not recognized. IDENT_CURRENT is a built-in function available in all supported versions of SQL Server. If you get an error, check for typos or that you are not using a different database system (e.g., MySQL or PostgreSQL) that lacks this function.

Frequently Asked Questions

What is the difference between IDENT_CURRENT, @@IDENTITY, and SCOPE_IDENTITY?

IDENT_CURRENT returns the last identity value for a table across all sessions and scopes. @@IDENTITY returns the last identity value generated in the current session, regardless of scope. SCOPE_IDENTITY returns the last identity value generated in the current scope (e.g., the current stored procedure or batch). Use SCOPE_IDENTITY to get the value from your own insert safely.

Can IDENT_CURRENT be used with a variable for the table name?

Yes, as long as the variable is a character data type. For example: DECLARE @t NVARCHAR(128) = 'dbo.MyTable'; SELECT IDENT_CURRENT(@t);. The variable must contain the schema-qualified table name.

Does IDENT_CURRENT require the table to have rows?

No. It returns the last identity value generated, even if the table is empty (for example, after deleting all rows). If no identity value has ever been generated, it returns NULL.

Is IDENT_CURRENT affected by transactions?

IDENT_CURRENT reads the last identity value generated, even if the transaction that generated it is still open and later rolls back. This means it can return a value that is not committed. For transactional consistency, use SCOPE_IDENTITY() or the OUTPUT clause within the transaction.

Can I use IDENT_CURRENT to get the next identity value?

No, IDENT_CURRENT returns the last value generated. To get the next value, you would need to consider the increment and seed, but there is no built-in function that returns the next value without inserting. You can use DBCC CHECKIDENT with NORESEED to see the current identity value, but that is not the same as the next value.

Does IDENT_CURRENT work with tables in other databases?

Yes, if you provide a fully qualified name: SELECT IDENT_CURRENT('OtherDB.dbo.YourTable');. You need permission on that table in the other database.