我不相信有any保证插入和删除中行的顺序 - 就像在不指定 ORDER BY 的情况下从任何表中选择都没有顺序保证一样。
我决定看看是否可以制作一个脚本来证明缺乏排序。在我的机器(SQL 2008 Dev)上,我可以重复运行以下脚本。它从插入和删除中输出 2 行。请注意,我们没有触及 ID 列,因此如果假设正确(它们以某种方式排序),那么相同的 ID 应该以相同的顺序出现。这里的情况并非如此。
首先,输出:
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)
以及产生这个的脚本:
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