Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««12345

Create DML trigger for single table in SQL 2008 and send email alert to group Expand / Collapse
Author
Message
Posted Saturday, April 26, 2014 9:33 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 5:06 PM
Points: 8,713, Visits: 9,259
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
Post #1565330
Posted Sunday, April 27, 2014 11:10 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 3:33 PM
Points: 52, Visits: 809
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.
Post #1565458
Posted Monday, April 28, 2014 2:59 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 3:33 PM
Points: 52, Visits: 809
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 ?
Post #1565713
Posted Monday, April 28, 2014 3:13 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:04 PM
Points: 13,296, Visits: 12,147
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)
Post #1565716
Posted Monday, April 28, 2014 5:06 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 5:06 PM
Points: 8,713, Visits: 9,259
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
Post #1565731
Posted Thursday, May 1, 2014 4:15 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 3:33 PM
Points: 52, Visits: 809
Sorry Sean & Tom ,

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

Post #1566869
« Prev Topic | Next Topic »

Add to briefcase «««12345

Permissions Expand / Collapse