Skip to content

Commit ce0c5bf

Browse files
authored
Acrolinx
1 parent 773a240 commit ce0c5bf

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

docs/t-sql/functions/scope-identity-transact-sql.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ manager: "jhubbard"
2828
# SCOPE_IDENTITY (Transact-SQL)
2929
[!INCLUDE[tsql-appliesto-ss2008-asdb-xxxx-xxx_md](../../includes/tsql-appliesto-ss2008-asdb-xxxx-xxx-md.md)]
3030

31-
Returns the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch. Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch.
31+
Returns the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch. Therefore, if two statements are in the same stored procedure, function, or batch, they are in the same scope.
3232

3333
![Topic link icon](../../database-engine/configure-windows/media/topic-link.gif "Topic link icon") [Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
3434

@@ -50,7 +50,7 @@ SCOPE_IDENTITY()
5050

5151
For example, there are two tables, T1 and T2, and an INSERT trigger is defined on T1. When a row is inserted to T1, the trigger fires and inserts a row in T2. This scenario illustrates two scopes: the insert on T1, and the insert on T2 by the trigger.
5252

53-
Assuming that both T1 and T2 have identity columns, @@IDENTITY and SCOPE_IDENTITY will return different values at the end of an INSERT statement on T1. @@IDENTITY will return the last identity column value inserted across any scope in the current session. This is the value inserted in T2. SCOPE_IDENTITY() will return the IDENTITY value inserted in T1. This was the last insert that occurred in the same scope. The SCOPE_IDENTITY() function will return the null value if the function is invoked before any INSERT statements into an identity column occur in the scope.
53+
Assuming that both T1 and T2 have identity columns, @@IDENTITY and SCOPE_IDENTITY return different values at the end of an INSERT statement on T1. @@IDENTITY returns the last identity column value inserted across any scope in the current session. This is the value inserted in T2. SCOPE_IDENTITY() returns the IDENTITY value inserted in T1. This was the last insert that occurred in the same scope. The SCOPE_IDENTITY() function returns the null value if the function is invoked before any INSERT statements into an identity column occur in the scope.
5454

5555
Failed statements and transactions can change the current identity for a table and create gaps in the identity column values. The identity value is never rolled back even though the transaction that tried to insert the value into the table is not committed. For example, if an INSERT statement fails because of an IGNORE_DUP_KEY violation, the current identity value for the table is still incremented.
5656

@@ -120,13 +120,14 @@ GO
120120

121121
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
122122
```
123+
/*SCOPE_IDENTITY returns the last identity value in the same scope. This was the insert on table TZ.*/`
123124
SCOPE_IDENTITY
124125
4
125-
/*SCOPE_IDENTITY returned the last identity value in the same scope. This was the insert on table TZ.*/`
126126
127+
/*@@IDENTITY returns the last identity value inserted to TY by the trigger.
128+
This fired because of an earlier insert on TZ.*/
127129
@@IDENTITY
128130
115
129-
/*@@IDENTITY returned the last identity value inserted to TY by the trigger. This fired because of an earlier insert on TZ.*/
130131
```
131132

132133
### B. Using @@IDENTITY and SCOPE_IDENTITY() with replication

0 commit comments

Comments
 (0)