MS SQL 2005 AFTER UPDATE 触发器更新更新以外的其他行

发布于 2024-09-27 14:31:56 字数 1260 浏览 2 评论 0原文

作为一个经验很少或没有经验的用户,我必须在表上创建一个触发器(或找到另一个解决方案)。需要完成的是,当表中另一行中 ESB 列的值更改为“1”时,该列必须设置为“0”。

我的目的是使用 AFTER UPDATE 触发器来执行此操作。

ALTER TRIGGER [TR_PHOTO_UPD]
   ON    [SOA].[dbo].[photos_TEST]
   AFTER  UPDATE
AS 

DECLARE @ID  VARCHAR(10)
DECLARE @ESB  VARCHAR(1)

SELECT @ID  = (SELECT TOP(1) ID 
      FROM SOA.dbo.photos_TEST 
               WHERE esb = 'Q' 
               ORDER BY ARRIVALDATETIME ASC)
SELECT @ESB (SELECT esb FROM INSERTED)


IF @ESB = '1'
UPDATE SOA.dbo.photos_TEST SET esb = '0' WHERE ID = @I

您可能明白,这不起作用,下一个触发器定义也不起作用。

ALTER TRIGGER [TR_PHOTO_UPD]
   ON    [SOA].[dbo].[photos_TEST]
   AFTER  UPDATE
AS 

DECLARE @ID  VARCHAR(10)
DECLARE @ESB  VARCHAR(1)

SELECT @ID  = (SELECT TOP(1) ID 
      FROM SOA.dbo.photos_TEST 
               WHERE esb = 'Q' 
               ORDER BY ARRIVALDATETIME ASC)
SELECT @ESB (SELECT esb FROM INSERTED)


    IF @ESB = '1'
    BEGIN
  UPDATE SOA.dbo.photos_TEST 
  SET esb = '0'
  WHERE id = (SELECT TOP(1) ID 
       FROM SOA.dbo.photos_TEST 
       WHERE esb = 'Q' 
       ORDER BY ARRIVALDATETIME ASC)
    END

经过几个小时的谷歌搜索和尝试后,我还没有找到为什么该行没有更新为“0”。我怀疑 AFTER UPDATE 触发器中的 UPDATE 是它不起作用的原因。有人有任何提示或更好的解决方案吗?

干杯,

彼得

As a user with little or no experience i have to create a trigger on a table (or find another solution). What needs to be accomplished is that when the value of the column ESB is changed to '1' in another row in the table the column has to be set to '0'.

My intention was to use a AFTER UPDATE trigger, to do this.

ALTER TRIGGER [TR_PHOTO_UPD]
   ON    [SOA].[dbo].[photos_TEST]
   AFTER  UPDATE
AS 

DECLARE @ID  VARCHAR(10)
DECLARE @ESB  VARCHAR(1)

SELECT @ID  = (SELECT TOP(1) ID 
      FROM SOA.dbo.photos_TEST 
               WHERE esb = 'Q' 
               ORDER BY ARRIVALDATETIME ASC)
SELECT @ESB (SELECT esb FROM INSERTED)


IF @ESB = '1'
UPDATE SOA.dbo.photos_TEST SET esb = '0' WHERE ID = @I

This as, you probably understand, does not work, neither the next trigger definition.

ALTER TRIGGER [TR_PHOTO_UPD]
   ON    [SOA].[dbo].[photos_TEST]
   AFTER  UPDATE
AS 

DECLARE @ID  VARCHAR(10)
DECLARE @ESB  VARCHAR(1)

SELECT @ID  = (SELECT TOP(1) ID 
      FROM SOA.dbo.photos_TEST 
               WHERE esb = 'Q' 
               ORDER BY ARRIVALDATETIME ASC)
SELECT @ESB (SELECT esb FROM INSERTED)


    IF @ESB = '1'
    BEGIN
  UPDATE SOA.dbo.photos_TEST 
  SET esb = '0'
  WHERE id = (SELECT TOP(1) ID 
       FROM SOA.dbo.photos_TEST 
       WHERE esb = 'Q' 
       ORDER BY ARRIVALDATETIME ASC)
    END

After several hours of googling and trying i have not yet found out why the row is not updated to '0'. I have a suspicion that the UPDATE in an AFTER UPDATE trigger is the reason why it does not work. Has somebody any hints or better a solution ?

Cheers,

Peter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

π浅易 2024-10-04 14:31:56

在处理多个更新时,这两种方法都不起作用。

为什么您甚至需要根据具体情况进行操作。无论如何,您不能在整个表中运行更新吗?

也许是这样的:

If Update(ESB)
    begin

       Update a
       Set AnotherColumn = 0
       From YourTable a
       Join inserted ins on a.Id = ins.Id
       Where ins.ESB = 1

    end

这会检查 ESB 列是否已更新。如果是,那么它会运行更新语句将 AnotherColumn 设置为零,其中 ESB 值为 1

我认为这应该适合您

Both of these approaches won't work when dealing with multiple updates.

Why do you even need to do it on an case by case basis. Can't you just run an update across the table anyway.

Maybe something along the lines of:

If Update(ESB)
    begin

       Update a
       Set AnotherColumn = 0
       From YourTable a
       Join inserted ins on a.Id = ins.Id
       Where ins.ESB = 1

    end

This checks to see if it was the ESB column that was updated. If it was then it runs the update statement to set AnotherColumn to zero where the ESB value is 1

I think that should work for you

匿名。 2024-10-04 14:31:56

感谢巴里的帮助才最终确定了触发;

    CREATE TRIGGER TR_PHOTO_AU
   ON          SOA.dbo.photos
   AFTER     UPDATE
AS 

DECLARE @MAXCONC INT  -- Maximum concurrent processes
DECLARE @CONC INT     -- Actual concurrent processes

SET @MAXCONC = 1      -- 1 concurrent process

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON

-- If column esb is involved in the update, does not necessarily mean
-- that the column itself is updated
If ( Update(ESB) )
BEGIN
    -- If column esb has been changed to 1
    IF ((SELECT esb FROM INSERTED) = '1')
    BEGIN
        -- count the number of (imminent) active processes
        SET @CONC = (SELECT COUNT(*) 
                  FROM SOA.dbo.photos pc
                  WHERE pc.esb in ('0','R'))

        -- if maximum has not been reached
        IF NOT ( @CONC >= @MAXCONC )
        BEGIN
            -- set additional rows esb to '0' to match @MAXCONC
               UPDATE TOP(@MAXCONC-@CONC) p2
               SET p2.esb = '0'
            FROM ( SELECT TOP(@MAXCONC-@CONC) p1.esb 
                   FROM SOA.dbo.photos  p1
                   WHERE  p1.esb = 'Q'
                   ORDER BY p1.arrivaldatetime ASC 

            ) p2

        END
    END
END

Thanks to the help of Barry if finalized the trigger;

    CREATE TRIGGER TR_PHOTO_AU
   ON          SOA.dbo.photos
   AFTER     UPDATE
AS 

DECLARE @MAXCONC INT  -- Maximum concurrent processes
DECLARE @CONC INT     -- Actual concurrent processes

SET @MAXCONC = 1      -- 1 concurrent process

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON

-- If column esb is involved in the update, does not necessarily mean
-- that the column itself is updated
If ( Update(ESB) )
BEGIN
    -- If column esb has been changed to 1
    IF ((SELECT esb FROM INSERTED) = '1')
    BEGIN
        -- count the number of (imminent) active processes
        SET @CONC = (SELECT COUNT(*) 
                  FROM SOA.dbo.photos pc
                  WHERE pc.esb in ('0','R'))

        -- if maximum has not been reached
        IF NOT ( @CONC >= @MAXCONC )
        BEGIN
            -- set additional rows esb to '0' to match @MAXCONC
               UPDATE TOP(@MAXCONC-@CONC) p2
               SET p2.esb = '0'
            FROM ( SELECT TOP(@MAXCONC-@CONC) p1.esb 
                   FROM SOA.dbo.photos  p1
                   WHERE  p1.esb = 'Q'
                   ORDER BY p1.arrivaldatetime ASC 

            ) p2

        END
    END
END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文