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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

@@ERROR on triggers???

Status
Not open for further replies.

shootsie

Programmer
Apr 12, 2001
43
US
Does anyone know how to trap an error in a trigger? I can get it to work in everything but a trigger. Something similar to this:

IF @@ERROR <> 0
PRINT 'Error Ignored'

Also helpful would be any way to ignore an error in a trigger. Thanks!

-Shootsie
 
Hi there,
I'm pretty sure you are allowed to use @@error in triggers. (Anyone: am I wrong on that?) So I'm wondering if there's any other issue we could be seeing here.

One thing you are probably aware of is that @@error only applies to the immediately preceding t-sql statement.

So for example:
INSERT statement.....with an error in it.
Print 'Hello'
Select @@error

Here, @@error would be zero, because the immediately preceding statement (print) worked.

Here's a more common example:
INSERT statement.....with an error in it.
set @myrowcount = @@rowcount
Select @@error

Again, @@error will not show an error, because the prior statement (set @myrowcount) worked.


Do you think this might bear on what you are seeing?

If so, to get around this, we usually capture @@error right away to a local variable, before any other statement can change its value:

declare @myerror
set @myerror = 0
INSERT statement.....with an error in it.
set @myerror = @@error
.....

Now you can use @myerror wherever you want.

Hope this helps a bit. But I feel sure you can use @@error in a trigger.

bp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top