簡體   English   中英

INSERT可以在觸發器“插入”表中產生多行嗎?

[英]Can an INSERT result in more than one row in a trigger “inserted” table?

我知道在一個觸發器中 - 至少對於SQL Server - 不應該假設inserted表只有一行,這意味着觸發器中的SQL通常是壞的:

select @UserID = ID from inserted

但出於好奇,一組INSERT語句是否會導致inserted多個行的表? 我知道使用UPDATE很容易,但從我的測試中我無法模擬INSERT的類似結果。 我已經嘗試在發送批處理終結符之前發送多組插入,例如:

insert into TriggerTest (col2) select 'a'
insert into TriggerTest (col2) select 'b'
insert into TriggerTest (col2) select 'c'
go

並將它們包裝在交易中:

begin tran
insert into TriggerTest (col2) select 'a'
insert into TriggerTest (col2) select 'b'
insert into TriggerTest (col2) select 'c'
commit

但它總是會導致觸發器在inserted的1行表中被觸發3次,而從未使用inserted的3行表觸發。

這對我來說是完全有意義的(畢竟它們是3個單獨的語句),我不需要實際執行它,我只是想知道單獨的INSERTS是否會對此行為不同。

編輯:這是一個愚蠢的問題:當然,插入結果集時可以!

insert into TriggerTest (col2) select 'a' union select 'b'

......或任何其他類型的集合。

原諒我,這里差不多凌晨3點。 我會把這個問題留給那些應該知道更好的人。

嘗試

insert into TriggerTest (col2) 
select 'a'
union 
select 'b'
union
select 'c'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM