是否可以保证“插入”和“删除”表在AFTER UPDATE触发器中以相同顺序返回其记录? [英] Are the 'inserted' and 'deleted' tables guaranteed to return their records in the same order in an AFTER UPDATE trigger?

查看:50
本文介绍了是否可以保证“插入”和“删除”表在AFTER UPDATE触发器中以相同顺序返回其记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有AFTER UPDATE触发器,将

  SELECT * FROM插入的

 选择*从已删除

以相同的顺序退还他们的记录吗?



<即可以说我能够对它们的结果集进行索引,即使复合主键的一个值发生了变化,
del [5]和ins [5]也会给我返回匹配的条目(这就是原因为什么内部联接不起作用)。

解决方案

我不相信有任何保证对插入和删除行中的行进行排序-就像没有从表中进行选择的排序保证一样,没有指定ORDER BY。






我决定看看是否可以生成一个脚本来证明缺乏排序。在我的机器上(SQL 2008 Dev),我可以重复地运行以下脚本。它从插入和删除中输出2行。请注意,我们不会触摸ID列,因此,如果假设正确(以某种方式排序),则相同的ID应该以相同的顺序出现。



首先,输出为:

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

(受影响的2行)

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

(受影响的2行)

生成此脚本的脚本:

 创建表T1 (
ID int不为null,
D1 datetime不为空,
V1 varchar(100)不为空,
约束PK_T1主键(D1,ID)

转到
创建索引IX_T T1(D1)
上的1_D1 go
插入T1(ID,D1,V1)
选择ID,DATEADD(day,ID-1,'20100101'),'text'
from(从sysobjects so1,sysobjects so2,sysobjects中选择(ROW_NUMBER()OVER(ORDER BY so1.id))t(ID)
go
在更新
之后在T1上创建触发器T_T1_U
开始
选择*从插入的
选择*从已删除的
结束
go
sp_configure'禁止触发器结果',0
go
重新配置
进行
更新T1集D1 = DATEADD(month,CASE WHEN DATEPART(month,D1)= 2 THEN 1 ELSE -1 END,D1)
其中D1 in(' 20100201','20100301')

sp_configure'禁止触发器结果',1

重新配置

放置表T1


If I have an AFTER UPDATE trigger, will

SELECT * FROM inserted

and

SELECT * FROM deleted

give me back their records in the same order?

I.e. lets say I was able to index into their result sets, will del[5] and ins[5] give me back the matching entries, even if one value of the compound primary key has changed (which would be the reason why an inner join won't work).

解决方案

I don't believe there is any guarantee over the ordering of rows within inserted and deleted - just as there is no ordering guarantee for selecting from any table, without specifying an ORDER BY.


I decided to see if I could produce a script where we could demonstrate the lack of ordering. On my machine (SQL 2008 Dev), I can repeatably run the following script. It outputs 2 rows from inserted and deleted. Note that we don't touch the ID column, and so if the supposition was correct (that they were ordered in some way), then the same IDs should appear in the same order. This is not the case here.

First, the outputs:

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)

And the script that produced this:

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

这篇关于是否可以保证“插入”和“删除”表在AFTER UPDATE触发器中以相同顺序返回其记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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