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 @@IDENTITY
scenario:
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 INSERT
AS
BEGIN
insert into test1 values('usa')
END;
CREATE TRIGGER Parentins ON test1 FOR INSERT
AS
BEGIN
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 test1
select * from test2
select 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..