if exists (select 1 
           from sysobjects
           where id=object_id('TNT_depot')
                 and type='TR')
           drop trigger TNT_depot
gocreate trigger TNT_depot on d_quota
for update
as
begin
      declare @mchid varchar(10)
      declare @get_mchid varchar(10)
    if update(chk_sure)
         begin           set @mchid=(select parentid from j_depot where depotid=(select get_depotid from inserted))
           set @get_mchid=(select get_mchid from inserted)
            if @get_mchid<>@mchid
              RAISERROR ('出错了!, 16, 1)
      ROLLBACK TRANSACTION
         end
end上面是我写的一个触发器,在进行单行更新的时候是没问题,而批量更新的时候,就会出问题。
请高手指定一下,应该怎么修改?
不胜感激。

解决方案 »

  1.   


    if exists (select 1 
              from sysobjects 
              where id=object_id('TNT_depot') 
                    and type='TR') 
              drop trigger TNT_depot 
    go create trigger TNT_depot on d_quota 
    for update 
    as 
    begin 
          declare @mchid varchar(10) 
          declare @get_mchid varchar(10) 
        if update(chk_sure) 
            begin       select @mchid=parentid from j_depot where depotid in (select get_depotid from inserted)) 
              select @get_mchid=get_mchid from inserted 
                if @get_mchid <>@mchid 
                  RAISERROR ('出错了!, 16, 1) 
          ROLLBACK TRANSACTION 
            end 
    end 
      

  2.   


    if exists (select 1 
              from sysobjects 
              where id=object_id('TNT_depot') 
                    and type='TR') 
              drop trigger TNT_depot 
    go create trigger TNT_depot on d_quota 
    for update 
    as 
    begin 
          declare @mchid varchar(10) 
          declare @get_mchid varchar(10) 
        if update(chk_sure) 
            begin       select @mchid=parentid from j_depot where depotid in (select get_depotid from inserted)) 
              select @get_mchid=get_mchid from inserted 
                if @get_mchid <>@mchid 
                  RAISERROR ('出错了!, 16, 1) 
          ROLLBACK TRANSACTION 
            end 
    end 
      

  3.   


    --这两行,子查询返回多个值
     set @mchid=(select parentid from j_depot where depotid=(select get_depotid from inserted))
              set @get_mchid=(select get_mchid from inserted)
      

  4.   


    if exists (select 1 
              from sysobjects 
              where id=object_id('TNT_depot') 
                    and type='TR') 
              drop trigger TNT_depot 
    go create trigger TNT_depot on d_quota 
    for update 
    as 
    begin 
          declare @mchid varchar(10) 
          declare @get_mchid varchar(10) 
        if update(chk_sure) 
            begin       select @mchid=parentid from j_depot where depotid in (select get_depotid from inserted)) 
              select @get_mchid=get_mchid from inserted 
                if @get_mchid <>@mchid 
                  RAISERROR ('出错了!, 16, 1) 
          ROLLBACK TRANSACTION 
            end 
    end 
      

  5.   

    这样写不行,都已经更新了rollvack已经不起作用
      

  6.   

    create trigger TNT_depot on d_quota 
    for update 
    as 
    begin 
        declare @count int
        if update(chk_sure) 
            begin 
            set @count=@@rowcount
            select 1 from (select parentid from j_depot where depotid in(select get_depotid from inserted)) aa inner join inserted bb on aa.parentid=bb.get_mchid
                if @@rowcount<>@count 
                begin
                  RAISERROR ('出错了!', 16, 1) 
                  ROLLBACK TRANSACTION 
                end
               end 
    end 
      

  7.   

    create trigger TNT_depot on d_quota
    for update
    as
    begin
    if update(chk_sure)
    if exists(select * from j_depot a join inserted b on a.depotid=b.get_depotid and a.parentid<>b.get_mchid)
    begin
    RAISERROR ('出错了!',16,1)
    ROLLBACK TRANSACTION
    end
        else
    end 这样比价好
      

  8.   

    create trigger TNT_depot on d_quota
    for update
    as
    begin
    if update(chk_sure)
    if exists(select * from j_depot a join inserted b on a.depotid=b.get_depotid and a.parentid<>b.get_mchid)
    begin
    RAISERROR ('出错了!',16,1)
    ROLLBACK TRANSACTION
    end
        else
    end 这样比价好
      

  9.   

    用八楼的方法,运行时报错了。
    但是看不到是哪里有问题。在关键字 'end' 附近有语法错误。
      

  10.   

    try:
    create trigger TNT_depot on d_quota
    for update
    as
    begin
    if update(chk_sure)
      begin
        if exists(select * from j_depot a join inserted b on a.depotid=b.get_depotid and a.parentid<>b.get_mchid)
            begin
                RAISERROR ('出错了!',16,1)
                ROLLBACK TRANSACTION
            end
       end
    end 
      

  11.   

    用八楼的方法,运行时报错了。 
    但是看不到是哪里有问题。 在关键字 'end' 附近有语法错误。
    ==========================================
    create trigger TNT_depot on d_quota
    for update
    as
    begin
    if update(chk_sure) begin /*****/
        if exists(select * from j_depot a join inserted b on a.depotid=b.get_depotid and a.parentid<>b.get_mchid) 
            begin
                RAISERROR ('出错了!',16,1)
                ROLLBACK TRANSACTION
            end
        else
    end 
      

  12.   

    create trigger TNT_depot on d_quota 
    for update 
    as 
        if NOT update(chk_sure) 
    RETURN
      
    IF EXISTS(
    SELECT 1
    FROM 
    j_depot A
    JOIN
    inserted I ON A.depotid=I.get_depotid
    WHERE
    I.get_mchid<>A.parentid)
    BEGIN
      RAISERROR (N'出错了!', 16, 1) 
    ROLLBACK TRANSACTION 
    END