我正在 SQL Server 2012 表上编写触发器。触发器本身工作正常,除了当我从触发器更新表后添加语句以发送电子邮件时,ELSE 语句会以错误突出显示
ELSE 附近的语法不正确
我可能有语法错误,但我无法指出。
我试图添加“;” 在第 10 行和第 11 行的末尾,但它没有起到作用。评论第 11 行将使整个触发器正常工作
在 IF 之后有两个语句/动作的最佳方法是什么?
1 CREATE TRIGGER dbo.MembersNameToCategories ON dbo.Members
2 AFTER UPDATE
3 AS
4
5 BEGIN
6 DECLARE @OldName NVARCHAR(150) = (SELECT name FROM Deleted)
7 DECLARE @NewName NVARCHAR(150) = (SELECT name FROM Inserted)
8 DECLARE @BodyTXT nvarchar(MAX) = 'Customer: ' + @oldName + ' has been modified with new name : ' +@NewName + '.'
9 IF EXISTS (SELECT NULL FROM Categories AS c WHERE c.Name = @OldName)
10 UPDATE Categories SET Name = @NewName WHERE Name = @OldName AND CategoryType = 7
11 EXEC msdb.dbo.sp_send_dbmail @profile_name = 'MyProfile',@recipients = 'tous@mycompany.ca',@body_format = 'HTML',@body = @BodyTXT,@subject = 'A customer name has been modified'
12 ELSE
13 INSERT INTO Categories (ID,Modified,ModifiedByID,CategoryType,Name,Colour,RANK,MemberType,Private,DefaultCategory,Description,SendNotification)
14 VALUES (
15 NEWID(),
16 GETDATE(),
17 dbo.fnmyid(),
18 7,
19 @NewName,
20 -8000,
21 9999,
22 0,
23 0,
24 0,
25 N'',
26 0
27 )
28 END;
29 GO
谢谢你-马丁