如果更新正在分配用户,我需要创建一个触发器。我们之前没有设定价值。
我有这个,但服务器不喜欢它....我收到错误
'附近的语法不正确。'
在'设置'线。
CREATE TRIGGER dbo.trgIssueAcknowledged
ON dbo.hdIssues
AFTER UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
If (select inserted.AssignedToUserID from inserted) IS NULL
begin
return
end
If (select inserted.AssignedToUserID from inserted) = 0
begin
return
end
If (select inserted.AcknowledgeDate from inserted) IS NULL
begin
set inserted.AcknowledgeDate = GETDATE ( );
end
END
GO
答案 0 :(得分:5)
你的触发器存在许多问题,它没有使用你想要更新的真实表,它假设每次只修改一行,而RETURN
的逻辑太奇怪。
CREATE TRIGGER dbo.trgIssueAcknowledged
ON dbo.hdIssues
AFTER UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
UPDATE A
SET A.AcknowledgeDate = GETDATE()
FROM dbo.hdIssues A
INNER JOIN INSERTED I
ON A.TheKeyOfTheTable = I.TheKeyOfTheTable
WHERE A.AcknowledgeDate IS NULL
AND ( A.AssignedToUserID <> 0 AND A.AssignedToUserID IS NOT NULL)
END
答案 1 :(得分:0)
这可能是拼写错误吗?
inserted.AcknoledgeDate
看起来很缺少&#34; w&#34;
应该是
inserted.AcknowledgeDate
答案 2 :(得分:0)
首先插入的是虚拟表,因此如果要更新其中一列,则需要进行更新。
UPDATE inserted
set AcknowledgeDate = GETDATE ( )
WHERE AcknowledgeDate IS NULL;
但第二,为什么要在AFTER UPDATE触发器中更新inserted表中的值?你真的想做什么?
答案 3 :(得分:0)
试试这个
CREATE TRIGGER dbo.trgIssueAcknowledged ON dbo.hdIssues
AFTER INSERT, DELETE, UPDATE
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
DECLARE @INSERTCOUNT INT
DECLARE @DELETECOUNT INT
SET @INSERTCOUNT = ( SELECT COUNT(*)
FROM INSERTED
)
SET @DELETECOUNT = ( SELECT COUNT(*)
FROM DELETED
)
IF @INSERTCOUNT > 0
AND @DELETECOUNT = 0
BEGIN
-- Insert statements for trigger here
INSERT INTO dbo.hdIssues
( col1, col2)
SELECT I.col1,
I.col2
FROM INSERTED I
END
ELSE
IF @INSERTCOUNT > 0
AND @DELETECOUNT > 0
BEGIN
INSERT INTO dbo.hdIssues
( col1, col2)
SELECT I.col1,
I.col2
FROM INSERTED I
INNER JOIN DELETED D ON I.col1 = D.col1
WHERE I.col1 <> D.col1
OR
I.col2 <> D.col2
END
END TRY
BEGIN CATCH
END CATCH
END
GO