代码之家  ›  专栏  ›  技术社区  ›  Gavin

INSERT可以在触发器“inserted”表中产生多行吗?

  •  4
  • Gavin  · 技术社区  · 14 年前

    inserted 表只有一行,这意味着触发器中这样的SQL通常是错误的:

    select @UserID = ID from inserted
    

    但出于好奇,一组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
    

    但它总是会导致触发器以 插入 一行一张桌子,从来没有一次 插入

    这对我来说是完全有意义的(毕竟它们是3个独立的语句),我不需要真的这么做,我只是想知道插入本身是否会有不同的表现。

    编辑: 这是一个愚蠢的问题:当然可以,当插入一个结果集!

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

    原谅我,已经快凌晨3点了。我将把这个问题留给那些应该更了解的人。

    1 回复  |  直到 14 年前
        1
  •  5
  •   Ed Harper    14 年前

    尝试

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