MS SQL 2005 AFTER UPDATE 触发器更新更新以外的其他行
作为一个经验很少或没有经验的用户,我必须在表上创建一个触发器(或找到另一个解决方案)。需要完成的是,当表中另一行中 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在处理多个更新时,这两种方法都不起作用。
为什么您甚至需要根据具体情况进行操作。无论如何,您不能在整个表中运行更新吗?
也许是这样的:
这会检查 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:
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 1I think that should work for you
感谢巴里的帮助才最终确定了触发;
Thanks to the help of Barry if finalized the trigger;