Create DML trigger for single table in SQL 2008 and send email alert to group

  • krishnavenkat16 (4/16/2013)


    ....but the requester is looking for old item i mean records before the update...

    You commented the code that does that out:

    if @optype = 2 --or @optype = 3

    the or @optype = 3 is what puts in the old (pre-update) values in the update case; the code I suggested gives both the pre-update values and the post-update values, so that you can see both what has been changed and what it has been changed to.

    The delete case having a null body was a silly omission in what I suggested. the declaration of body needs to be changed to

    DECLARE @Body varchar(500); set @Body = '';

    so that it isn't null even if there are no new rows.

    Tom

  • Thanks Steve,Lowell,L'eomot for your help and patience ....script is working fine now....i'm posting the entire script for people who might need it in future....

    --/****** Object: Table [dbo].[logger] Script Date: 04/15/2013 13:07:57 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    SET ANSI_PADDING ON

    GO

    CREATE TABLE [dbo].[logger_all](

    [Body] [varchar](500) NULL,

    [ConfigSetID_Ins] [varchar](100) NULL,

    ConfigSetID_Del varchar(100),

    [Subject] [varchar](104) NULL,

    [Name] [varchar](200) NULL,

    [Version] [varchar](100) NULL,

    [Timestamp] [varchar](100) NULL

    ) ON [PRIMARY]

    GO

    SET ANSI_PADDING OFF

    GO

    CREATE TRIGGER [dbo].[ConfigTrigger_New]

    ON [dbo].[ConfigSet]

    AFTER Update, insert, delete

    AS

    DECLARE @ConfigSetID_Ins varchar(100)

    DECLARE @ConfigSetID_Del varchar(100)

    DECLARE @Body varchar(500)

    Declare @Subject varchar(104)

    Declare @Name varchar(200)

    Declare @Version varchar(100)

    Declare @Timestamp varchar(100)

    declare @optype tinyint = 0;

    if exists (select * from inserted) set @optype = @optype+1

    if exists (select * from deleted) set @optype = @optype+2

    BEGIN

    set @Subject =

    case @optype

    when 1 then 'New row inserted into ConfigSet table in XXXXXX'

    when 2 then 'Row deleted from ConfigSet table in XXXXXXX'

    when 3 then 'Row modified in ConfigSet table in XXXXXX'

    else 'Nothing Happened'

    end ;

    SELECT @ConfigSetID_Ins = i.ConfigSetID, @Name = i.Name, @Version = .[Version],

    @Timestamp = i.[Timestamp]

    from inserted i --join ConfigSet C on I.ConfigSetID = C.ConfigSetID

    SELECT @ConfigSetID_Del = d.ConfigSetID, @Name = d.Name, @Version = [d].[Version],

    @Timestamp = d.[Timestamp]

    from deleted d-- join ConfigSet C on d.ConfigSetID = C.ConfigSetID

    if @optype = 1 or @optype = 3

    select @Body = 'New record has been Updated in ConfigSet table in XXXXXX, Following are the details ' + char(13)

    + 'ConfigSetID : ' + ISNULL(@ConfigSetID_Ins,'[Missing ConfigSet]') + CHAR(13)

    + 'Name : ' + ISNULL(@Name ,'[Missing Name]') + CHAR(13)

    + 'Version : ' + ISNULL(@Version,'[Missing Version]') + CHAR(13)

    + 'TimeStamp : ' + ISNULL(@Timestamp,'[Missing Timestamp]') + CHAR(13)

    from inserted i ;

    if @optype = 2 --or @optype = 3

    select @Body = 'New record has been Updated in ConfigSet table in XXXXX, Following are the details ' + char(13)

    + 'ConfigSetID : ' + ISNULL(@ConfigSetID_Del,'[Missing ConfigSet]') + CHAR(13)

    + 'Name : ' + ISNULL(@Name ,'[Missing Name]') + CHAR(13)

    + 'Version : ' + ISNULL(@Version,'[Missing Version]') + CHAR(13)

    + 'TimeStamp : ' + ISNULL(@Timestamp,'[Missing Timestamp]') + CHAR(13)

    from deleted d ;

    INSERT INTO dbo.logger_all SELECT @Body, @ConfigSetID_Ins, @ConfigSetID_Del, @Subject, @Name, @Version, @Timestamp

    EXEC msdb..sp_send_dbmail

    @profile_name = 'XXXXXX',

    @recipients = 'XXXX@.com',

    @subject = @subject,

    @body = @body

    END

  • krishnavenkat16 (4/16/2013)


    Thanks Steve,Lowell,L'eomot for your help and patience ....script is working fine now....i'm posting the entire script for people who might need it in future....

    Actually with that code you are getting the new values for updates and not the old values. Maybe that's not a problem?

    Tom

  • Hey Tom,

    Here what i'm getting....

    for insert...new records are logged - with this requester can know what record is inserted

    for update....old records are logged - with this Requester will know what has been updated

    for delete ....deleted records are logged - with this Requester will know what has been deleted

    fyi, i have removed the comment on @optype = 3

    i guess this result is fine....please let me know if i need to update the code...

  • krishnavenkat16 (4/16/2013)


    Hey Tom,

    Here what i'm getting....

    for insert...new records are logged - with this requester can know what record is inserted

    for update....old records are logged - with this Requester will know what has been updated

    for delete ....deleted records are logged - with this Requester will know what has been deleted

    fyi, i have removed the comment on @optype = 3

    i guess this result is fine....please let me know if i need to update the code...

    Yes, removing that comnment fixes it. I don't see any other problem. Glad you've got something that works - it took a while for us to help you there.

    Tom

  • Yes, thank you very much ..

  • Thank you for this DML trigger.

    When I am running this trigger I am getting syntax error is there any thing I need to change in that trigger ?

  • dba.sql29 (4/25/2014)


    Thank you for this DML trigger.

    When I am running this trigger I am getting syntax error is there any thing I need to change in that trigger ?

    There shouldn't be a syntax error as it worked for Robin35. Have you changed anything?

    Anyway, if you tell us what the error message is maybe we can help.

    Tom

  • Ah, sorry. I was doing a little test and hit post instead of prview.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • Robin35 (4/16/2013)


    Thanks Steve,Lowell,L'eomot for your help and patience ....script is working fine now....i'm posting the entire script for people who might need it in future....

    --/****** Object: Table [dbo].[logger] Script Date: 04/15/2013 13:07:57 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    SET ANSI_PADDING ON

    GO

    CREATE TABLE [dbo].[logger_all](

    [Body] [varchar](500) NULL,

    [ConfigSetID_Ins] [varchar](100) NULL,

    ConfigSetID_Del varchar(100),

    [Subject] [varchar](104) NULL,

    [Name] [varchar](200) NULL,

    [Version] [varchar](100) NULL,

    [Timestamp] [varchar](100) NULL

    ) ON [PRIMARY]

    GO

    SET ANSI_PADDING OFF

    GO

    CREATE TRIGGER [dbo].[ConfigTrigger_New]

    ON [dbo].[ConfigSet]

    AFTER Update, insert, delete

    AS

    DECLARE @ConfigSetID_Ins varchar(100)

    DECLARE @ConfigSetID_Del varchar(100)

    DECLARE @Body varchar(500)

    Declare @Subject varchar(104)

    Declare @Name varchar(200)

    Declare @Version varchar(100)

    Declare @Timestamp varchar(100)

    declare @optype tinyint = 0;

    if exists (select * from inserted) set @optype = @optype+1

    if exists (select * from deleted) set @optype = @optype+2

    BEGIN

    set @Subject =

    case @optype

    when 1 then 'New row inserted into ConfigSet table in XXXXXX'

    when 2 then 'Row deleted from ConfigSet table in XXXXXXX'

    when 3 then 'Row modified in ConfigSet table in XXXXXX'

    else 'Nothing Happened'

    end ;

    SELECT @ConfigSetID_Ins = i.ConfigSetID, @Name = i.Name, @Version = .[Version],

    @Timestamp = i.[Timestamp]

    from inserted i --join ConfigSet C on I.ConfigSetID = C.ConfigSetID

    SELECT @ConfigSetID_Del = d.ConfigSetID, @Name = d.Name, @Version = [d].[Version],

    @Timestamp = d.[Timestamp]

    from deleted d-- join ConfigSet C on d.ConfigSetID = C.ConfigSetID

    if @optype = 1 or @optype = 3

    select @Body = 'New record has been Updated in ConfigSet table in XXXXXX, Following are the details ' + char(13)

    + 'ConfigSetID : ' + ISNULL(@ConfigSetID_Ins,'[Missing ConfigSet]') + CHAR(13)

    + 'Name : ' + ISNULL(@Name ,'[Missing Name]') + CHAR(13)

    + 'Version : ' + ISNULL(@Version,'[Missing Version]') + CHAR(13)

    + 'TimeStamp : ' + ISNULL(@Timestamp,'[Missing Timestamp]') + CHAR(13)

    from inserted i ;

    if @optype = 2 --or @optype = 3

    select @Body = 'New record has been Updated in ConfigSet table in XXXXX, Following are the details ' + char(13)

    + 'ConfigSetID : ' + ISNULL(@ConfigSetID_Del,'[Missing ConfigSet]') + CHAR(13)

    + 'Name : ' + ISNULL(@Name ,'[Missing Name]') + CHAR(13)

    + 'Version : ' + ISNULL(@Version,'[Missing Version]') + CHAR(13)

    + 'TimeStamp : ' + ISNULL(@Timestamp,'[Missing Timestamp]') + CHAR(13)

    from deleted d ;

    INSERT INTO dbo.logger_all SELECT @Body, @ConfigSetID_Ins, @ConfigSetID_Del, @Subject, @Name, @Version, @Timestamp

    EXEC msdb..sp_send_dbmail

    @profile_name = 'XXXXXX',

    @recipients = 'XXXX@.com',

    @subject = @subject,

    @body = @body

    END

    Actually, while I'm here...

    If I'm reading the code correctly, the code has a bit of a flaw in it that you might want to consider. If someone inserts, updates, or deletes more than 1 row in the same insert, update, or delete, what do you want to be contained in the email? I suspect it will be that you want more than 1 row?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • Jeff Moden (4/25/2014)


    Actually, while I'm here...

    If I'm reading the code correctly, the code has a bit of a flaw in it that you might want to consider. If someone inserts, updates, or deletes more than 1 row in the same insert, update, or delete, what do you want to be contained in the email? I suspect it will be that you want more than 1 row?

    I'm pretty sure the OP was basing everything on the idea that all inserts, updates, and deletes were single row. Certainly Lowell's suggestion on 11 April last year handled multiple rows (including serialization using XML) and my post on April 15 last year (at 8:17 PM) specifically pointed out the multiple row issue and provided code that handled it with plain text serialization, but the OP payed no attention to that aspect of those posts. I'd forgotten that the OP had not adopted anything to handle multiple rows, hence my not very helpful response to dba.sql29.

    Both statements in this part of the code

    SELECT @ConfigSetID_Ins = i.ConfigSetID, @Name = i.Name, @Version = .[Version],

    @Timestamp = i.[Timestamp]

    from inserted i --join ConfigSet C on I.ConfigSetID = C.ConfigSetID

    SELECT @ConfigSetID_Del = d.ConfigSetID, @Name = d.Name, @Version = [d].[Version],

    @Timestamp = d.[Timestamp]

    from deleted d-- join ConfigSet C on d.ConfigSetID = C.ConfigSetID

    will fail if more than one row is updated, inserted, or deleted so that may be where dba.sql29 is finding a problem.

    Tom

  • Thanks for reply ,

    I am getting below error when I am trying to create the trigger.Only thing I changed is inserting output into a different database on same server. I don't want any emails to be sent.

    This is the error I am getting.

    Msg 207, Level 16, State 1, Procedure ConfigTrigger_New, Line 51

    Invalid column name 'ConfigSetID_Ins'.

    Msg 207, Level 16, State 1, Procedure ConfigTrigger_New, Line 51

    Invalid column name 'Name'.

    Msg 207, Level 16, State 1, Procedure ConfigTrigger_New, Line 51

    Invalid column name 'Version'.

    Msg 207, Level 16, State 1, Procedure ConfigTrigger_New, Line 52

    Invalid column name 'Timestamp'.

    Msg 207, Level 16, State 1, Procedure ConfigTrigger_New, Line 55

    Invalid column name 'ConfigSetID'.

    Msg 207, Level 16, State 1, Procedure ConfigTrigger_New, Line 55

    Invalid column name 'Name'.

    Msg 207, Level 16, State 1, Procedure ConfigTrigger_New, Line 55

    Invalid column name 'Version'.

    Msg 207, Level 16, State 1, Procedure ConfigTrigger_New, Line 56

    Invalid column name 'Timestamp'.

    Msg 213, Level 16, State 1, Procedure ConfigTrigger_New, Line 76

    Column name or number of supplied values does not match table definition.

    Just made one change

    --SET ANSI_NULLS ON

    --GO

    --SET QUOTED_IDENTIFIER ON

    --GO

    --SET ANSI_PADDING ON

    --GO

    --CREATE TABLE [dbo].[logger_all](

    --[Body] [varchar](500) NULL,

    --[ConfigSetID_Ins] [varchar](100) NULL,

    --ConfigSetID_Del varchar(100),

    --[Subject] [varchar](104) NULL,

    --[Name] [varchar](200) NULL,

    --[Version] [varchar](100) NULL,

    --[Timestamp] [varchar](100) NULL

    --) ON [PRIMARY]

    --GO

    --SET ANSI_PADDING OFF

    --GO

    CREATE TRIGGER [dbo].[ConfigTrigger_New]

    ON [dbo].

    AFTER Update, insert, delete

    AS

    DECLARE @ConfigSetID_Ins varchar(100)

    DECLARE @ConfigSetID_Del varchar(100)

    DECLARE @Body varchar(500)

    Declare @Subject varchar(104)

    Declare @Name varchar(200)

    Declare @Version varchar(100)

    Declare @Timestamp varchar(100)

    Declare @optype tinyint = 0;

    if exists (select * from inserted) set @optype = @optype+1

    if exists (select * from deleted) set @optype = @optype+2

    BEGIN

    set @Subject =

    case @optype

    when 1 then 'New row inserted into [dbo].

    in database'

    when 2 then 'Row deleted from [dbo].

    in database'

    when 3 then 'Row modified in [dbo].

    in database'

    else 'Nothing Happened'

    end ;

    SELECT @ConfigSetID_Ins = i.ConfigSetID_Ins, @Name = i.Name, @Version = .[Version],

    @Timestamp = i.[Timestamp]

    from inserted i --join ConfigSet C on I.ConfigSetID = C.ConfigSetID

    SELECT @ConfigSetID_Del = d.ConfigSetID, @Name = d.Name, @Version = [d].[Version],

    @Timestamp = d.[Timestamp]

    from deleted d-- join ConfigSet C on d.ConfigSetID = C.ConfigSetID

    if @optype = 1 or @optype = 3

    select @Body = 'New record has been Updated in [dbo].

    table in database, Following are the details ' + char(13)

    + 'ConfigSetID : ' + ISNULL(@ConfigSetID_Ins,'[Missing ConfigSet]') + CHAR(13)

    + 'Name : ' + ISNULL(@Name ,'[Missing Name]') + CHAR(13)

    + 'Version : ' + ISNULL(@Version,'[Missing Version]') + CHAR(13)

    + 'TimeStamp : ' + ISNULL(@Timestamp,'[Missing Timestamp]') + CHAR(13)

    from inserted i ;

    if @optype = 2 --or @optype = 3

    select @Body = 'New record has been Updated in [dbo].

    table in Database, Following are the details ' + char(13)

    + 'ConfigSetID : ' + ISNULL(@ConfigSetID_Del,'[Missing ConfigSet]') + CHAR(13)

    + 'Name : ' + ISNULL(@Name ,'[Missing Name]') + CHAR(13)

    + 'Version : ' + ISNULL(@Version,'[Missing Version]') + CHAR(13)

    + 'TimeStamp : ' + ISNULL(@Timestamp,'[Missing Timestamp]') + CHAR(13)

    from deleted d ;

    INSERT INTO CMMonitor.dbo.CommandLog -- Different database

    select @ConfigSetID_Ins,@ConfigSetID_Del,@Body,@Subject,@Name,@Version,@Timestamp,@optype

    end

    --EXEC msdb..sp_send_dbmail

    --@profile_name = 'XXXXXX',

    --@recipients = 'XXXX@.com',

    --@subject = @subject,

    --@body = @body

    --END

    Thanks all for your help.

  • All ,

    Thanks for your help.

    This trigger is not going to work for me. I am trying to capture user name, data and couple of other thing when something is inserted, deleted or updated into a particulate table.

    Does any one know that can be possible ?

  • dba.sql29 (4/28/2014)


    All ,

    Thanks for your help.

    This trigger is not going to work for me. I am trying to capture user name, data and couple of other thing when something is inserted, deleted or updated into a particulate table.

    Does any one know that can be possible ?

    Here is the problem. This thread was created by somebody else over a year ago. You are now hijacking that thread. You should start your own thread to get help with your problem.

    The second issue is that you have not provided us much detail here.

    Before you start your own thread please do yourself a favor and take a few minutes to read the first article in my signature about best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • dba.sql29 (4/28/2014)


    All ,

    Thanks for your help.

    This trigger is not going to work for me. I am trying to capture user name, data and couple of other thing when something is inserted, deleted or updated into a particulate table.

    Does any one know that can be possible ?

    The errors you listed are all to do with table structure - the original trigger uses teh column names of teh original table, and what it does depends on the number of coulmns in the original table. You need to adjust it to use the shape and column names (and types) of your table.

    As sean said, you would best start a ne topic to deal with your particular question. The things that you are wanting to do are all fairly straightforward, but you need to provide information specific to your question for any of us to provide a detailed answer, and that specific information, since it contradicts the starting point information for this thread, should be placed in a new separate thread.

    Tom

Viewing 15 posts - 31 through 45 (of 45 total)

You must be logged in to reply to this topic. Login to reply