Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
TomThomson
TomThomson
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10640 Visits: 11980
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 = [i].[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

dba.sql29
dba.sql29
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 1205
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].[table]
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[s] inserted into [dbo].[table] in database'
when 2 then 'Row[s] deleted from [dbo].[table] in database'
when 3 then 'Row[s] modified in [dbo].[table] in database'
else 'Nothing Happened'
end ;

SELECT @ConfigSetID_Ins = i.ConfigSetID_Ins, @Name = i.Name, @Version = [i].[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] 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] 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.
dba.sql29
dba.sql29
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 1205
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 ?
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16512 Visits: 16985
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
TomThomson
TomThomson
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10640 Visits: 11980
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

dba.sql29
dba.sql29
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 1205
Sorry Sean & Tom ,

I will start a new thread with a details.
again sorry.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search