触发拒绝多行更新

时间:2014-12-01 16:27:17

标签: sql-server triggers sql-server-2012

我的触发器中有一个测试,它适用于1行,但我无法弄清楚如何在多行进行编辑时拒绝更新。

当前代码:

CREATE TRIGGER forecasting.sitenametest
ON forecasting.sitenames
AFTER UPDATE
AS
  BEGIN
      if @@ROWCOUNT = 0
        return

      SET nocount ON;

      DECLARE @test INT
      DECLARE @SiteTest CHAR(8)

      SELECT @SiteTest = i.sitename FROM inserted i

      SET @test = CASE
                WHEN @SiteTest = 'NA      ' THEN 1
                WHEN OtherTest..            THEN 1
                ELSE 0
            END

      IF @test = 0
      BEGIN
          ROLLBACK
          RAISERROR('Site code not in correct format',16,1)
      END
END

1 个答案:

答案 0 :(得分:0)

如何再次使用rowcount进行测试:

CREATE TRIGGER forecasting.sitenametest ON forecasting.sitenames
    AFTER UPDATE
AS
    BEGIN
        IF @@ROWCOUNT = 0
            RETURN

        SET NOCOUNT ON;

        SELECT  *
        FROM    inserted
        WHERE   sitename IN ( 'NA      ', 'Other Examples' )

        IF @@ROWCOUNT > 0
            BEGIN
                ROLLBACK
                RAISERROR('Site code not in correct format',16,1)
            END

    END