我正在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
谢谢 -Martin
答案 0 :(得分:0)
试试这个:
IF EXISTS (SELECT NULL FROM Categories AS c WHERE c.Name = @OldName)
BEGIN
UPDATE Categories SET Name = @NewName WHERE Name = @OldName AND CategoryType = 7
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'
END
ELSE