Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ul> <li><p><a href="http://msdn.microsoft.com/en-us/library/ms187342.aspx" rel="noreferrer"><code>@@IDENTITY</code></a> returns the last identity value generated for any table in the current session, across all scopes. <strong>You need to be careful here</strong>, since it's across scopes. You could get a value from a trigger, instead of your current statement.</p></li> <li><p><a href="http://msdn.microsoft.com/en-us/library/ms190315.aspx" rel="noreferrer"><code>SCOPE_IDENTITY()</code></a> returns the last identity value generated for any table in the current session and the current scope. <strong>Generally what you want to use</strong>.</p></li> <li><p><a href="http://msdn.microsoft.com/en-us/library/ms175098.aspx" rel="noreferrer"><code>IDENT_CURRENT('tableName')</code></a> returns the last identity value generated for a specific table in any session and any scope. This lets you specify which table you want the value from, in case the two above aren't quite what you need (<strong>very rare</strong>). Also, as @<a href="https://stackoverflow.com/questions/42648/best-way-to-get-identity-of-inserted-row#42665">Guy Starbuck</a> mentioned, "You could use this if you want to get the current IDENTITY value for a table that you have not inserted a record into."</p></li> <li><p>The <a href="http://msdn.microsoft.com/en-us/library/ms177564.aspx" rel="noreferrer"><code>OUTPUT</code> clause</a> of the <code>INSERT</code> statement will let you access every row that was inserted via that statement. Since it's scoped to the specific statement, it's <strong>more straightforward</strong> than the other functions above. However, it's a little <strong>more verbose</strong> (you'll need to insert into a table variable/temp table and then query that) and it gives results even in an error scenario where the statement is rolled back. That said, if your query uses a parallel execution plan, this is the <strong>only guaranteed method</strong> for getting the identity (short of turning off parallelism). However, it is executed <em>before</em> triggers and cannot be used to return trigger-generated values.</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload