我創建了一個觸發器來更新另一個表中的行,由dbo.update
中的更新語句觸發。此觸發器失敗的原因可能是因爲它試圖更新多於一條記錄。我試圖改變這一點,但沒有結果。我想知道:做這件事的正確方法是什麼?子查詢在觸發器上返回了多個值
CREATE TRIGGER
[dbo].[Update]
ON
[dbo].[Records]
FOR UPDATE
AS
BEGIN
SET NOCOUNT ON
IF (SELECT State FROM INSERTED) = '1'
Begin
UPDATE dbo.Issue SET state = 1 WHERE EventID = (SELECT EventID FROM INSERTED)
DECLARE @IssueID INT
SET @issueID = (SELECT IssueID FROM Issue WHERE EventID = (SELECT EventID FROM INSERTED))
UPDATE dbo.Action SET state = 1 WHERE IssueID = @issueID
end
IF (SELECT State FROM INSERTED) = '0'
Begin
UPDATE dbo.Issue SET state = 0 WHERE EventID = (SELECT EventID FROM INSERTED)
SET @issueID = (SELECT IssueID FROM Issue WHERE EventID = (SELECT EventID FROM INSERTED))
UPDATE dbo.Action SET state = 0 WHERE IssueID = @issueID
end
END
'IF(SELECT State FROM INSERTED)='1'' ...使用遊標讀取表格。 – jarlh