Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking whether a trigger executed successfully

Status
Not open for further replies.

FALCONSEYE

Programmer
Jul 30, 2004
1,158
US
is there a way to check if an update/insert trigger executes successfully? for some reason, it seems like my trigger sometimes works and sometimes doesn't. is there a log file that records if the execution failed?
thanks in advance

 
How 'bout posting trigger code here?

------
Theory: everybody knows everything, nothing works
Practice: everything works, nobody knows why

[banghead]
 
you can check for @@Error in the trigger itself for errors and writing suser_sname, ID, getdate() etc etc to an errorlogtable
also you can have an if condition by checking the @@rowcount after your trigger action and writing those id's to a table so that you can investigate

Denis The SQL Menace
SQL blog:
Personal Blog:
 
this is the trigger

CREATE TRIGGER customerOptStatus
ON Customer
FOR INSERT, UPDATE

AS

declare @email varchar(100)

BEGIN

IF EXISTS
(
SELECT i.emailAddress
FROM Inserted i, Customer c
WHERE i.emailAddress = c.emailAddress
AND c.optInCD = 'no'
AND i.optInCD = 'yes'
)
BEGIN

SELECT @email = emailAddress FROM Inserted

UPDATE Customer
SET optStatus = 'NO'
WHERE emailAddress = @email
END

END





 
one way
Code:
CREATE TRIGGER customerOptStatus
ON Customer
FOR INSERT, UPDATE

AS 

declare @email varchar(100)
declare @errorId int,@Rowcount int
BEGIN

    IF EXISTS 
    (
        SELECT i.emailAddress 
        FROM Inserted i, Customer c
        WHERE i.emailAddress = c.emailAddress
        AND   c.optInCD = 'no'
                AND   i.optInCD = 'yes'
    )
    BEGIN
        
        SELECT @email = emailAddress FROM Inserted

        UPDATE Customer
        SET optStatus = 'NO'
        WHERE emailAddress = @email
	select @errorId=@@error,@Rowcount =@@ROWCOUNT
	if @errorId <> 0
	begin
		insert into errorlogtable
		select 'some error has occured email logged =' + @email,getdate(),suser_sname,@email
	end
	else
	if @Rowcount =0
	begin
		insert into errorlogtable
		select 'No records have been updated fro some bizzare reason email logged =' + @email,getdate(),suser_sname,@email
	end
    END
    
END
of course you will need to create the errorlogtable table

Denis The SQL Menace
SQL blog:
Personal Blog:
 
Are multirow inserts/updates possible or not?

------
Theory: everybody knows everything, nothing works
Practice: everything works, nobody knows why

[banghead]
 
Hold on like vongrunt said your problem might be with multi row updates in that case you have to change
Code:
UPDATE Customer
        SET optStatus = 'NO'
        WHERE emailAddress = @email
to

Code:
UPDATE c
        SET c.ptStatus = 'NO'
from Customer c 
join Inserted i on i.emailAddress = c.emailAddress
AND   c.optInCD = 'no'
AND   i.optInCD = 'yes'

Denis The SQL Menace
SQL blog:
Personal Blog:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top