sql - How to use two statements in a trigger -


i'm writing trigger on sql server 2012 table. trigger working correctly except when add statement send email after updating table trigger else statement highlighted error

incorrect syntax near else

i might have syntax error can't point out.

i've tried add ";" @ end of line 10 , 11 it's not doing trick. commenting line 11 make whole trigger work correctly

what best method have 2 statement/action after if ?

1 create trigger dbo.membersnametocategories on dbo.members 2 after update 3  4  5 begin 6   declare @oldname nvarchar(150) = (select name deleted) 7   declare @newname nvarchar(150) = (select name inserted) 8   declare @bodytxt nvarchar(max) = 'customer: ' + @oldname + ' has been modified new name : ' +@newname + '.' 9   if exists (select null  categories c c.name = @oldname) 10      update categories set name = @newname name = @oldname , 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 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 

thank -martin

try this:

if exists (select null  categories c c.name = @oldname) begin       update categories set name = @newname name = @oldname , 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 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -