If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Can I catch the error in a stored procedure.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-01-04, 05:56
AStefan AStefan is offline
Registered User
 
Join Date: Jun 2004
Posts: 57
Can I catch the error in a stored procedure.

I have some insert procedures like the folowing (written in SQL Server):

create procedure Insert_Item (@1, @2 etc)
as

declare @error int

insert into Item (field1, field2, ...)
values (@1, @2, ...)
select @error = @@error

if @error <> 0
exec Handle_errors (Object_Name, Table_Name, etc)

IS it possible to catch the error in Db2 in a similar manner?
Thanks for your support.
Reply With Quote
  #2 (permalink)  
Old 07-01-04, 08:12
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Yes,
Look in the "SQL Refernece" manual under "Compound Statement (Procedure)"
for the correct syntax. Look particularly at the handler-declaration section.

HTH

Andy
Reply With Quote
  #3 (permalink)  
Old 07-02-04, 00:59
AStefan AStefan is offline
Registered User
 
Join Date: Jun 2004
Posts: 57
Catching and handling the sqlstate value.

Sorry if I wasn't very precise.
I lost all my yesterday tryiing to resolve this problem.
I have a custom procedure handle_errors. I have a parameter in this procedure who stands for the original error value (which correspond to @@error in sql server and to sqlstate in DB2).
I would like to catch the sqlstate value and in the handle to call my custom procedure having sqlstate as parameter, but I couldn't.
I sqw that I can handle the errors one by one (FK violation errors, unique violations errors, each of them having a sql state). But I was wondering if it is not possible to catch the sql state value and to have a single handle for sql state and in this handle to get sql state as parameter.
Reply With Quote
  #4 (permalink)  
Old 07-02-04, 08:18
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
I believe that you want something like this:

DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS EXCEPTION 1 MESSAGE_TEXT_OUT = MESSAGE_TEXT;
SELECT SQLSTATE, SQLCODE INTO SQLSTATE_OUT, SQLCODE_OUT
FROM SYSIBM.SYSDUMMY1;

ROLLBACK;
END;

SQLEXCEPTION will catch all SQLSTATEs starting with "00", "01", or "02"

HTH

Andy
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On