Are "inserted" and "deleted" tables guaranteed to return their records in the same order to the AFTER UPDATE trigger?

If I have an AFTER UPDATE trigger,

SELECT * FROM inserted

and

SELECT * FROM deleted

return your records in the same order?

those. let's say I was able to index their result sets, del [5] and ins [5] will return matching records even if one value of the composite primary key has changed (which would cause the inner join to not work).

+3
source share
2 answers

, - - , ORDER BY.


, script, . (SQL 2008 Dev) script. 2 . , , , ( - ), . .

:

ID          D1                      V1
----------- ----------------------- ----------------------------------------------------------------------------------------------------
32          2010-03-01 00:00:00.000 text
60          2010-02-01 00:00:00.000 text

(2 row(s) affected)

ID          D1                      V1
----------- ----------------------- ----------------------------------------------------------------------------------------------------
60          2010-03-01 00:00:00.000 text
32          2010-02-01 00:00:00.000 text

(2 row(s) affected)

script, :

create table T1 (
    ID int not null,
    D1 datetime not null,
    V1 varchar(100) not null,
    constraint PK_T1 PRIMARY KEY (D1,ID)
)
go
create index IX_T1_D1 on T1(D1)
go
insert into T1(ID,D1,V1)
select ID,DATEADD(day,ID-1,'20100101'),'text'
from (select ROW_NUMBER() OVER (ORDER BY so1.id) from sysobjects so1,sysobjects so2,sysobjects) t(ID)
go
create trigger T_T1_U on T1 after update
as
begin
    select * from inserted
    select * from deleted
end
go
sp_configure 'disallow results from triggers',0
go
RECONFIGURE
go
update T1 set D1 = DATEADD(month,CASE WHEN DATEPART(month,D1)=2 THEN 1 ELSE -1 END,D1)
where D1 in ('20100201','20100301')
go
sp_configure 'disallow results from triggers',1
go
RECONFIGURE
go
drop table T1
go
+4

PRIMARY KEY ( , id), .

-2

Source: https://habr.com/ru/post/1774936/


All Articles