April 6, 2005 at 10:27 am
here's the trigger code. Effectively, I want SQL to send me an email everytime someone changes an instrument's associated country to 'QQ'. This works great when a developer does so in Query Analyzer, however, this is rarely the case. Is there any way to get the name of the calling application or ideally, the Windows login of the user that caused the trigger to fire? I could have sworn I saw this data in SQL Profiler at some point...
thanks in advance.
Greg
-----------------
ALTER trigger trgInstGregTemp
on tbInst
for insert, update as
declare @newCountry int
declare @SevernTicker varchar(128)
declare @InstsAffected int
declare @EditedByName varchar(128)
declare @messagebody varchar(1024)
select @InstsAffected = count(*)
from inserted i
if @InstsAffected = 1
select @newCountry = i.InstCountryID,
@SevernTicker = i.SevernTicker,
@EditedByName = EP.EtcPersonName
from inserted i
inner join tbEtcPerson EP
on i.EditorID = EP.EtcPersonID
if @newCountry = 1
begin
set @SevernTicker = 'SevernTicker: ' + @SevernTicker + ' -- country updated to ''QQ'' by ' + ISNULL(@EditedByName, 'unknown')
exec master..xp_sendmail
@recipients = 'greg.johnson@severnrivercapital.com; jeff.francis@severnrivercapital.com',
@message = @SevernTicker,
@subject = 'bad country assigned to Instrument'
end
---------------------------
April 6, 2005 at 11:24 am
Use @@SPID to join to master..sysprocesses.
There are functions for returning specific items, but this is the old fashioned way I always use.
April 6, 2005 at 12:51 pm
rockin suggestion john. Here's my uber-quality @message now...
SevernTicker: cyCAD -- country updated to 'QQ'
NTLOGIN: greg johnson
MACHINE: CT-DEV-05
PROGRAM: SQL Query Anal
April 7, 2005 at 6:40 am
another pointer...you should never have a trigger actually send an email; it can take several seconds for the email step to occur, and if there is a network or other mail server error that raises an unexpected error, you could cause the trigger to fail.
you should have the trigger insert a record into a separate table with the email information, and that table is processed by a scheduled job to send any items that have not been flagged as successfully sent. getting that job to run every few minutes or whatever is appropriate.
Lowell
April 7, 2005 at 8:06 am
Is there anyway to get the name/id of the stored procedure that caused the trigger to fire?
April 7, 2005 at 10:54 am
IF
Then:
Change the SPs as:
--at the begining of the proc
Declare @data varbinary(128)
SET @data = CAST( @@PROCID as binary(4) )
SET CONTEXT_INFO @data
........
<the proc code >
......
-- at the end
SET CONTEXT_INFO 0x0
On the Trigger:
Declare @Calling_Proc varchar(128)
select @Calling_Proc = object_name(substring( p.context_info, 1, 4 ) ) from master..sysprocesses as p WHERE p.spid = @@SPID
hth
* Noel
April 7, 2005 at 12:36 pm
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply