Home All Groups Group Topic Archive Search About

handle errors in trigger

Author
13 Jan 2006 10:21 AM
Vikram
How to handle error in trigger.
i m inserting record in other table in after insert trigger. but if insert
statement in trigger has error, procedure quits with error and i cannot use
@@error to check it

Author
13 Jan 2006 10:41 AM
Jens
Thats an artifical error, but that the way you could handle it:

Create Table SomeTable
(
        SomeColumn INT
)

Go

ALTER  TRIGGER SomeTrigger
on SomeTable
FOR INSERT
AS
BEGIN

RAISERROR('SomeError',16,1)

PRINT @@ERROR

IF @@ERROR <> 0
        PRINT 'Seems that an error occured.'

END


INSERT INTO SomeTable VALUES (1)

DELETE FROM SomeTable

SELECT * FROM SomeTable


Keep in mind that some error aren´catchable due to the severity.

I suggest taking a look in Erland´s Error handling script
http://www.sommarskog.se/error-handling-I.html#triggercontext.

HTH, jens Suessmeyer.
Author
13 Jan 2006 10:55 AM
Gerard
I don't know exactly what you're doing but there are a couple of things
you may want to check before you do an insert.

If you're using variables in your values list check them before you
insert if they aren't null in case your table has colums with not null
defined.

But in general do have a look at Erland's article.
Author
13 Jan 2006 11:46 AM
Vikram
Ya variable is null and table does not allow null, but error should be
trapable isnt it ?

Show quote
"Gerard" <g.doesw***@gmail.com> wrote in message
news:1137149721.665336.187300@g44g2000cwa.googlegroups.com...
> I don't know exactly what you're doing but there are a couple of things
> you may want to check before you do an insert.
>
> If you're using variables in your values list check them before you
> insert if they aren't null in case your table has colums with not null
> defined.
>
> But in general do have a look at Erland's article.
>
Author
13 Jan 2006 12:47 PM
Gerard
Error handling in sql server 2000 within triggers is tricky business,
have a look at Erland's article.

http://www.sommarskog.se/error-handling-I.html#triggercontext.

But since you know that the variable is null then something like this
may avoid the error:

DECLARE @var int

SET @var = ISNULL(@var, 1) /* the variable will be assigned the value
of 1 if it is null

or

IF(@var) IS NULL
BEGIN
          -- write all variables to a log table
          INSERT INTO ....

          RETURN
END
Author
13 Jan 2006 1:04 PM
ML
The AFTER trigger fires *after* the insert - hence the name. In case of a
constraint violation (such as the nullability constraint) the the AFTER
trigger will not fire at all, since the insert is aborted.

Consider using an INSTEAD OF trigger or better yet - set values properly
before inserting them or use a deafult.


ML

---
http://milambda.blogspot.com/
Author
13 Jan 2006 1:35 PM
Gerard
who says the variable is coming from inserted?
Author
13 Jan 2006 1:45 PM
ML
I was guessing based on OP's narrative.


ML

---
http://milambda.blogspot.com/
Author
13 Jan 2006 1:59 PM
Gerard
from OP
Show quote
> but if insert statement in trigger
Author
13 Jan 2006 2:00 PM
Gerard
Show quote
> but if insert statement in trigger
Author
13 Jan 2006 2:10 PM
ML
Exactly. And I'm not referring to the INSERTED virtual table. What am I
missing?


ML

---
http://milambda.blogspot.com/
Author
13 Jan 2006 2:33 PM
Gerard
also from OP

> Ya variable is null and table does not allow null

he's using the insert trigger to insert into another table, where he
get's the variable from I don't know
Author
13 Jan 2006 2:58 PM
ML
Perhaps Vikram can clarify this if he ever comes back. :)


ML

---
http://milambda.blogspot.com/

AddThis Social Bookmark Button