Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 451243b

Browse files
authored
Merge pull request #2341 from BYHAM/patch-308
Scope identity, fixing example formatting.
2 parents 54c4cf9 + dbc258a commit 451243b

1 file changed

Lines changed: 150 additions & 168 deletions

File tree

Lines changed: 150 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "SCOPE_IDENTITY (Transact-SQL) | Microsoft Docs"
33
ms.custom: ""
4-
ms.date: "03/03/2017"
4+
ms.date: "07/06/2017"
55
ms.prod: "sql-non-specified"
66
ms.reviewer: ""
77
ms.suite: ""
@@ -28,170 +28,152 @@ 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.
32-
33-
![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)
34-
35-
## Syntax
36-
37-
```
38-
39-
SCOPE_IDENTITY()
40-
```
41-
42-
## Return Types
43-
**numeric(38,0)**
44-
45-
## Remarks
46-
SCOPE_IDENTITY, IDENT_CURRENT, and @@IDENTITY are similar functions because they return values that are inserted into identity columns.
47-
48-
IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the value generated for a specific table in any session and any scope. For more information, see [IDENT_CURRENT (Transact-SQL)](../../t-sql/functions/ident-current-transact-sql.md).
49-
50-
SCOPE_IDENTITY and @@IDENTITY return the last identity values that are generated in any table in the current session. However, SCOPE_IDENTITY returns values inserted only within the current scope; @@IDENTITY is not limited to a specific scope.
51-
52-
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.
53-
54-
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.
55-
56-
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.
57-
58-
## Examples
59-
60-
### A. Using @@IDENTITY and SCOPE_IDENTITY with triggers
61-
The following example creates two tables, `TZ` and `TY`, and an INSERT trigger on `TZ`. When a row is inserted to table `TZ`, the trigger (`Ztrig`) fires and inserts a row in `TY`.
62-
63-
```
64-
USE tempdb;
65-
GO
66-
CREATE TABLE TZ (
67-
Z_id int IDENTITY(1,1)PRIMARY KEY,
68-
Z_name varchar(20) NOT NULL);
69-
70-
INSERT TZ
71-
VALUES ('Lisa'),('Mike'),('Carla');
72-
73-
SELECT * FROM TZ;
74-
75-
--Result set: This is how table TZ looks.
76-
```
77-
78-
`Z_id Z_name`
79-
80-
`-------------`
81-
82-
`1 Lisa`
83-
84-
`2 Mike`
85-
86-
`3 Carla`
87-
88-
```
89-
CREATE TABLE TY (
90-
Y_id int IDENTITY(100,5)PRIMARY KEY,
91-
Y_name varchar(20) NULL);
92-
93-
INSERT TY (Y_name)
94-
VALUES ('boathouse'), ('rocks'), ('elevator');
95-
96-
SELECT * FROM TY;
97-
--Result set: This is how TY looks:
98-
```
99-
100-
`Y_id Y_name`
101-
102-
`---------------`
103-
104-
`100 boathouse`
105-
106-
`105 rocks`
107-
108-
`110 elevator`
109-
110-
```
111-
/*Create the trigger that inserts a row in table TY
112-
when a row is inserted in table TZ.*/
113-
CREATE TRIGGER Ztrig
114-
ON TZ
115-
FOR INSERT AS
116-
BEGIN
117-
INSERT TY VALUES ('')
118-
END;
119-
120-
/*FIRE the trigger and determine what identity values you obtain
121-
with the @@IDENTITY and SCOPE_IDENTITY functions.*/
122-
INSERT TZ VALUES ('Rosalie');
123-
124-
SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY];
125-
GO
126-
SELECT @@IDENTITY AS [@@IDENTITY];
127-
GO
128-
129-
```
130-
131-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
132-
133-
`SCOPE_IDENTITY`
134-
135-
`4`
136-
137-
`/*SCOPE_IDENTITY returned the last identity value in the same scope. This was the insert on table TZ.*/`
138-
139-
`@@IDENTITY`
140-
141-
`115`
142-
143-
`/*@@IDENTITY returned the last identity value inserted to TY by the trigger. This fired because of an earlier insert on TZ.*/`
144-
145-
### B. Using @@IDENTITY and SCOPE_IDENTITY() with replication
146-
The following examples show how to use `@@IDENTITY` and `SCOPE_IDENTITY()` for inserts in a database that is published for merge replication. Both tables in the examples are in the [!INCLUDE[ssSampleDBobject](../../includes/sssampledbobject-md.md)] sample database: `Person.ContactType` is not published, and `Sales.Customer` is published. Merge replication adds triggers to tables that are published. Therefore, `@@IDENTITY` can return the value from the insert into a replication system table instead of the insert into a user table.
147-
148-
The `Person.ContactType` table has a maximum identity value of 20. If you insert a row into the table, `@@IDENTITY` and `SCOPE_IDENTITY()` return the same value.
149-
150-
```
151-
USE AdventureWorks2012;
152-
GO
153-
INSERT INTO Person.ContactType ([Name]) VALUES ('Assistant to the Manager');
154-
GO
155-
SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY];
156-
GO
157-
SELECT @@IDENTITY AS [@@IDENTITY];
158-
GO
159-
160-
```
161-
162-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
163-
164-
`SCOPE_IDENTITY`
165-
166-
`21`
167-
168-
`@@IDENTITY`
169-
170-
`21`
171-
172-
The `Sales.Customer` table has a maximum identity value of 29483. If you insert a row into the table, `@@IDENTITY` and `SCOPE_IDENTITY()` return different values. `SCOPE_IDENTITY()` returns the value from the insert into the user table, whereas `@@IDENTITY` returns the value from the insert into the replication system table. Use `SCOPE_IDENTITY()` for applications that require access to the inserted identity value.
173-
174-
```
175-
INSERT INTO Sales.Customer ([TerritoryID],[PersonID]) VALUES (8,NULL);
176-
GO
177-
SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY];
178-
GO
179-
SELECT @@IDENTITY AS [@@IDENTITY];
180-
GO
181-
182-
```
183-
184-
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
185-
186-
`SCOPE_IDENTITY`
187-
188-
`29484`
189-
190-
`@@IDENTITY`
191-
192-
`89`
193-
194-
## See Also
195-
[@@IDENTITY (Transact-SQL)](../../t-sql/functions/identity-transact-sql.md)
196-
197-
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.
32+
33+
![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)
34+
35+
## Syntax
36+
37+
```
38+
SCOPE_IDENTITY()
39+
```
40+
41+
## Return Types
42+
**numeric(38,0)**
43+
44+
## Remarks
45+
SCOPE_IDENTITY, IDENT_CURRENT, and @@IDENTITY are similar functions because they return values that are inserted into identity columns.
46+
47+
IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the value generated for a specific table in any session and any scope. For more information, see [IDENT_CURRENT (Transact-SQL)](../../t-sql/functions/ident-current-transact-sql.md).
48+
49+
SCOPE_IDENTITY and @@IDENTITY return the last identity values that are generated in any table in the current session. However, SCOPE_IDENTITY returns values inserted only within the current scope; @@IDENTITY is not limited to a specific scope.
50+
51+
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.
52+
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.
54+
55+
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.
56+
57+
## Examples
58+
59+
### A. Using @@IDENTITY and SCOPE_IDENTITY with triggers
60+
The following example creates two tables, `TZ` and `TY`, and an INSERT trigger on `TZ`. When a row is inserted to table `TZ`, the trigger (`Ztrig`) fires and inserts a row in `TY`.
61+
62+
```sql
63+
USE tempdb;
64+
GO
65+
CREATE TABLE TZ (
66+
Z_id int IDENTITY(1,1)PRIMARY KEY,
67+
Z_name varchar(20) NOT NULL);
68+
69+
INSERT TZ
70+
VALUES ('Lisa'),('Mike'),('Carla');
71+
72+
SELECT * FROM TZ;
73+
```
74+
Result set: This is how table TZ looks.
75+
76+
```
77+
Z_id Z_name
78+
-------------
79+
1 Lisa
80+
2 Mike
81+
3 Carla
82+
```
83+
```sql
84+
CREATE TABLE TY (
85+
Y_id int IDENTITY(100,5)PRIMARY KEY,
86+
Y_name varchar(20) NULL);
87+
88+
INSERT TY (Y_name)
89+
VALUES ('boathouse'), ('rocks'), ('elevator');
90+
91+
SELECT * FROM TY;
92+
```
93+
Result set: This is how TY looks:
94+
```
95+
Y_id Y_name
96+
---------------
97+
100 boathouse
98+
105 rocks
99+
110 elevator
100+
```
101+
102+
Create the trigger that inserts a row in table TY when a row is inserted in table TZ.
103+
```sql
104+
CREATE TRIGGER Ztrig
105+
ON TZ
106+
FOR INSERT AS
107+
BEGIN
108+
INSERT TY VALUES ('')
109+
END;
110+
```
111+
FIRE the trigger and determine what identity values you obtain with the @@IDENTITY and SCOPE_IDENTITY functions.
112+
```sql
113+
INSERT TZ VALUES ('Rosalie');
114+
115+
SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY];
116+
GO
117+
SELECT @@IDENTITY AS [@@IDENTITY];
118+
GO
119+
```
120+
121+
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
122+
```
123+
/*SCOPE_IDENTITY returns the last identity value in the same scope. This was the insert on table TZ.*/`
124+
SCOPE_IDENTITY
125+
4
126+
127+
/*@@IDENTITY returns the last identity value inserted to TY by the trigger.
128+
This fired because of an earlier insert on TZ.*/
129+
@@IDENTITY
130+
115
131+
```
132+
133+
### B. Using @@IDENTITY and SCOPE_IDENTITY() with replication
134+
The following examples show how to use `@@IDENTITY` and `SCOPE_IDENTITY()` for inserts in a database that is published for merge replication. Both tables in the examples are in the [!INCLUDE[ssSampleDBobject](../../includes/sssampledbobject-md.md)] sample database: `Person.ContactType` is not published, and `Sales.Customer` is published. Merge replication adds triggers to tables that are published. Therefore, `@@IDENTITY` can return the value from the insert into a replication system table instead of the insert into a user table.
135+
136+
The `Person.ContactType` table has a maximum identity value of 20. If you insert a row into the table, `@@IDENTITY` and `SCOPE_IDENTITY()` return the same value.
137+
138+
```sql
139+
USE AdventureWorks2012;
140+
GO
141+
INSERT INTO Person.ContactType ([Name]) VALUES ('Assistant to the Manager');
142+
GO
143+
SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY];
144+
GO
145+
SELECT @@IDENTITY AS [@@IDENTITY];
146+
GO
147+
```
148+
149+
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
150+
```
151+
SCOPE_IDENTITY
152+
21
153+
@@IDENTITY
154+
21
155+
```
156+
157+
The `Sales.Customer` table has a maximum identity value of 29483. If you insert a row into the table, `@@IDENTITY` and `SCOPE_IDENTITY()` return different values. `SCOPE_IDENTITY()` returns the value from the insert into the user table, whereas `@@IDENTITY` returns the value from the insert into the replication system table. Use `SCOPE_IDENTITY()` for applications that require access to the inserted identity value.
158+
159+
```sql
160+
INSERT INTO Sales.Customer ([TerritoryID],[PersonID]) VALUES (8,NULL);
161+
GO
162+
SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY];
163+
GO
164+
SELECT @@IDENTITY AS [@@IDENTITY];
165+
GO
166+
```
167+
168+
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
169+
```
170+
SCOPE_IDENTITY
171+
29484
172+
@@IDENTITY
173+
89
174+
```
175+
176+
## See Also
177+
[@@IDENTITY (Transact-SQL)](../../t-sql/functions/identity-transact-sql.md)
178+
179+

0 commit comments

Comments
 (0)