名称varchar(50) ) 创建表格test2 ( id int identity(1,1), name varchar(50) ) CREATE TRIGGER Parentinss ON test1 FOR INSERT AS BEGIN 插入test1值('usa') END; CREATE TRIGGER Parentins ON test1 FOR INSERT AS BEGIN 插入test2值('india') END; 插入test1值('ANURAG') 插入test1值('abhishek') 插入test2值('japan ') 插入test1值('china') select * from test1 select * from test2 选择SCOPE_IDENTITY() 选择@@ IDENTITY - - 有两个触发器。现在两个触发器都在test1上。一个触发器插入test1,另一个触发器插入test2。 - 选择SCOPE_IDENTITY() 选择@@ IDENTITY @@ identity给我输出为7。为什么? 为什么它不能给我6. 因为两个触发器都在test1中插入一个而在test2中插入另一个触发器。它如何选择7.即触发父元素[trigger]优于parentinns [trigger]。这是疑问..What is the difference between scope, identity() and @@identity?Can you give me an lucid example?As far as i know that both returns last generated identity column.doubt regarding @@IDENTITYscenario:create table test1(id int identity(1,1),name varchar(50))create table test2(id int identity(1,1),name varchar(50))CREATE TRIGGER Parentinss ON test1 FOR INSERTASBEGIN insert into test1 values('usa')END;CREATE TRIGGER Parentins ON test1 FOR INSERTASBEGIN insert into test2 values('india')END;insert into test1 values('ANURAG')insert into test1 values('abhishek')insert into test2 values('japan')insert into test1 values('china')select * from test1select * from test2select SCOPE_IDENTITY()select @@IDENTITY--There are two triggers. Now both triggers is on test1. one trigger inserts into test1 and the other on test2.--select SCOPE_IDENTITY()select @@IDENTITY@@identity gives me the output as 7 . why ?why doesnt it give me 6.As both the triggers are there which inserts one in test1 and the other in test2. How it selects 7. that is trigger parentins[trigger] is preferred over parentinns[trigger]. This is the doubt..推荐答案见这里: @@ IDENTITY,SCOPE_IDENTITY,IDENT_CURRENT 之间的区别[ ^ ]See here: Difference between @@IDENTITY, SCOPE_IDENTITY, IDENT_CURRENT[^]请阅读MSDN文档: @@IDENTITY(Transact-SQL) [ ^ ] SCOPE_IDENTITY(Transact-SQL) [ ^ ] 在那里你会找到答案;)Please, read MSDN documentation:@@IDENTITY (Transact-SQL)[^]SCOPE_IDENTITY (Transact-SQL)[^]There you'll find an answer ;) 这篇关于范围标识和@@标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-16 08:09