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 «««1234

Create DML trigger for single table in SQL 2008 and send email alert to group Expand / Collapse
Author
Message
Posted Tuesday, April 16, 2013 9:45 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 8:37 AM
Points: 8,287, Visits: 8,738
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
Post #1442821
Posted Tuesday, April 16, 2013 10:46 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 9:18 AM
Points: 78, Visits: 658
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[s] inserted into ConfigSet table in XXXXXX'
when 2 then 'Row[s] deleted from ConfigSet table in XXXXXXX'
when 3 then 'Row[s] modified in ConfigSet table in XXXXXX'
else 'Nothing Happened'
end ;

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

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




Post #1442860
Posted Tuesday, April 16, 2013 11:10 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 8:37 AM
Points: 8,287, Visits: 8,738
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
Post #1442876
Posted Tuesday, April 16, 2013 11:49 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 9:18 AM
Points: 78, Visits: 658
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...
Post #1442899
Posted Tuesday, April 16, 2013 1:57 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 8:37 AM
Points: 8,287, Visits: 8,738
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
Post #1442992
Posted Wednesday, April 17, 2013 7:09 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 9:18 AM
Points: 78, Visits: 658
Yes, thank you very much ..
Post #1443243
« Prev Topic | Next Topic »

Add to briefcase «««1234

Permissions Expand / Collapse