@@ Identity和Scope_Identity之间的区别?请以示例回答。 [英] Difference between @@Identity And Scope_Identity? Please answer with example.

查看:114
本文介绍了@@ Identity和Scope_Identity之间的区别?请以示例回答。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@@ Identity和Scope_Identity之间的区别?请以示例回答。

Difference between @@Identity And Scope_Identity? Please answer with example.

推荐答案

参见 @@ IDENTITY,SCOPE_IDENTITY,IDENT_CURRENT之间的区别 [ ^ ]。


@@ IDENTITY和SCOPE_IDENTITY返回在任何表中生成的最后一个标识值本届会议。但是,SCOPE_IDENTITY仅在当前范围内返回值; @@ IDENTITY不限于特定范围。
@@IDENTITY and SCOPE_IDENTITY return the last identity value generated in any table in the current session. However, SCOPE_IDENTITY returns the value only within the current scope; @@IDENTITY is not limited to a specific scope.


@@ IDENTITY返回在同一会话中创建的最后一个身份

其中as SCOPE_IDENTITY()返回最后一个在同一会话和相同范围内创建的标识。请参阅下面的示例以便更好地理解

@@IDENTITY Returns the last identity created in the same session
where as SCOPE_IDENTITY() Returns the last identity created in the same session and the same scope. See below example for better understanding
CREATE TABLE Table1
(
  ID_col1 INT IDENTITY(1,1),
  text_col1 VARCHAR(100)
)
GO
CREATE TABLE Table2
(
  ID_col2 INT IDENTITY(5,5),
  text_col2 VARCHAR(100)
)
GO
CREATE TRIGGER InsertValuesInTable1 
ON Table1 AFTER INSERT AS
BEGIN
  INSERT INTO Table2 (text_col2) VALUES ('some text for table 2')
  INSERT INTO Table2 (text_col2) SELECT text_col1 FROM inserted
END

INSERT INTO Table1 (text_col1) VALUES ('some text for table 1')
--SEE THE DIFFERENCE HERE BY RUNNING THIS QUERY
SELECT SCOPE_IDENTITY(), @@IDENTITY


这篇关于@@ Identity和Scope_Identity之间的区别?请以示例回答。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆