我正在编写一个触发器,以便每当shareInterval更改时,nextShareDate会自动更新。
nextShareDate =今天的日期+ shareInterval天。
Table: dbo.repeatShares
| repeatShareID | shareInterval | nextShareDate |
-------------------------------------------------
| 1 | 7 | 06/23/2014 |
-------------------------------------------------
| 2 | 14 | 06/30/2014 |
-------------------------------------------------
| (many other rows in this table) |
这是我到目前为止所拥有的。它不起作用;每当我更改shareInterval时,nextShareDate根本不会更改。谢谢!
USE [GFpermanent]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[trg_updateNextShareDate]
ON [dbo].[repeatShares]
AFTER UPDATE
AS
IF UPDATE(shareInterval) RETURN; --Don't react recursively
UPDATE
dbo.repeatShares
SET
nextShareDate = DATEADD(day,shareInterval,getDate())
WHERE
repeatShareID in (select repeatShareID from inserted)
答案 0 :(得分:0)
您需要更改在更改shareInterval时退出的行以下
IF UPDATE(shareInterval)RETURN;
到
IF UPDATE(nextShareDate)RETURN;
这将避免递归。