473,883 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Declaring a Condition handler in DB2 Stored Proc

Trying to declare a simple handler for SQLEXCEPTION.
Simple stored proc (UDB 8.2)
----
CREATE PROCEDURE TFBUDB.SMACF_SM _DI (IN POL_NBR CHAR(10)
, OUT ErrNo INTEGER
, OUT ErrMsg CHAR(80) )
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
P1: BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE NOTFOUND INTEGER DEFAULT 100;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
SET ErrMsg = 'Houston, we have a problem';

DECLARE D1 CURSOR WITH RETURN TO CALLER FOR
SELECT DI_NME_TXT
,DI_OCCR_NBR
FROM TFBUDB.SM_DI_NM E_INFO
WHERE POL_NBR_TXT = POL_NBR;
OPEN D1;
END P1
--------------------------------------------------------
Attempting to build in DB2 Development center I get the error below -
it certainly does not like it. Syntax for handler right out of book:

TFBUDB.SMACF_SM _DI - Build started.
DROP SPECIFIC PROCEDURE TFBUDB.SQL07041 3170923800
TFBUDB.SMACF_SM _DI - Drop stored procedure completed.
Create stored procedure returns -104.
TFBUDB.SMACF_SM _DI: 20: [IBM][CLI Driver][DB2/NT] SQL0104N An
unexpected token "<cursor declaration>" was found following "".
Expected tokens may include: "<SQL statement>". LINE NUMBER=20.
SQLSTATE=42601

TFBUDB.SMACF_SM _DI - Build failed.
TFBUDB.SMACF_SM _DI - Roll back completed successfully.

Apr 18 '07 #1
10 16475
On Apr 18, 2:39 pm, PJack...@txfb-ins.com wrote:
Trying to declare a simple handler for SQLEXCEPTION.
Simple stored proc (UDB 8.2)
----
CREATE PROCEDURE TFBUDB.SMACF_SM _DI (IN POL_NBR CHAR(10)
, OUT ErrNo INTEGER
, OUT ErrMsg CHAR(80) )
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
P1: BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE NOTFOUND INTEGER DEFAULT 100;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
SET ErrMsg = 'Houston, we have a problem';

DECLARE D1 CURSOR WITH RETURN TO CALLER FOR
SELECT DI_NME_TXT
,DI_OCCR_NBR
FROM TFBUDB.SM_DI_NM E_INFO
WHERE POL_NBR_TXT = POL_NBR;
OPEN D1;
END P1
--------------------------------------------------------
Attempting to build in DB2 Development center I get the error below -
it certainly does not like it. Syntax for handler right out of book:

TFBUDB.SMACF_SM _DI - Build started.
DROP SPECIFIC PROCEDURE TFBUDB.SQL07041 3170923800
TFBUDB.SMACF_SM _DI - Drop stored procedure completed.
Create stored procedure returns -104.
TFBUDB.SMACF_SM _DI: 20: [IBM][CLI Driver][DB2/NT] SQL0104N An
unexpected token "<cursor declaration>" was found following "".
Expected tokens may include: "<SQL statement>". LINE NUMBER=20.
SQLSTATE=42601

TFBUDB.SMACF_SM _DI - Build failed.
TFBUDB.SMACF_SM _DI - Roll back completed successfully.
There's an order that cursors, handlers, DGTTs, and variables must be
declared in. I'm not at my desk with my manuals and cheat sheets (so
I'm not 100% on this), but try putting your handler declaration after
your cursor declaration (but before the open).

--Jeff

Apr 18 '07 #2
On Apr 18, 2:39 pm, PJack...@txfb-ins.com wrote:
Trying to declare a simple handler for SQLEXCEPTION.
Simple stored proc (UDB 8.2)
----
CREATE PROCEDURE TFBUDB.SMACF_SM _DI (IN POL_NBR CHAR(10)
, OUT ErrNo INTEGER
, OUT ErrMsg CHAR(80) )
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
P1: BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE NOTFOUND INTEGER DEFAULT 100;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
SET ErrMsg = 'Houston, we have a problem';

DECLARE D1 CURSOR WITH RETURN TO CALLER FOR
SELECT DI_NME_TXT
,DI_OCCR_NBR
FROM TFBUDB.SM_DI_NM E_INFO
WHERE POL_NBR_TXT = POL_NBR;
OPEN D1;
END P1
--------------------------------------------------------
Attempting to build in DB2 Development center I get the error below -
it certainly does not like it. Syntax for handler right out of book:

TFBUDB.SMACF_SM _DI - Build started.
DROP SPECIFIC PROCEDURE TFBUDB.SQL07041 3170923800
TFBUDB.SMACF_SM _DI - Drop stored procedure completed.
Create stored procedure returns -104.
TFBUDB.SMACF_SM _DI: 20: [IBM][CLI Driver][DB2/NT] SQL0104N An
unexpected token "<cursor declaration>" was found following "".
Expected tokens may include: "<SQL statement>". LINE NUMBER=20.
SQLSTATE=42601

TFBUDB.SMACF_SM _DI - Build failed.
TFBUDB.SMACF_SM _DI - Roll back completed successfully.
OK--back at my desk. Here's the declaration order:

1. Variables
2. Conditions
3. Cursors
4. Handlers
5. DGTTs

--Jeff

Apr 19 '07 #3
jefftyzzer wrote:
On Apr 18, 2:39 pm, PJack...@txfb-ins.com wrote:
>Trying to declare a simple handler for SQLEXCEPTION.
Simple stored proc (UDB 8.2)
----
CREATE PROCEDURE TFBUDB.SMACF_SM _DI (IN POL_NBR CHAR(10)
, OUT ErrNo INTEGER
, OUT ErrMsg CHAR(80) )
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
P1: BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE NOTFOUND INTEGER DEFAULT 100;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
SET ErrMsg = 'Houston, we have a problem';

DECLARE D1 CURSOR WITH RETURN TO CALLER FOR
SELECT DI_NME_TXT
,DI_OCCR_NBR
FROM TFBUDB.SM_DI_NM E_INFO
WHERE POL_NBR_TXT = POL_NBR;
OPEN D1;
END P1
--------------------------------------------------------
Attempting to build in DB2 Development center I get the error below -
it certainly does not like it. Syntax for handler right out of book:

TFBUDB.SMACF_S M_DI - Build started.
DROP SPECIFIC PROCEDURE TFBUDB.SQL07041 3170923800
TFBUDB.SMACF_S M_DI - Drop stored procedure completed.
Create stored procedure returns -104.
TFBUDB.SMACF_S M_DI: 20: [IBM][CLI Driver][DB2/NT] SQL0104N An
unexpected token "<cursor declaration>" was found following "".
Expected tokens may include: "<SQL statement>". LINE NUMBER=20.
SQLSTATE=426 01

TFBUDB.SMACF_S M_DI - Build failed.
TFBUDB.SMACF_S M_DI - Roll back completed successfully.

OK--back at my desk. Here's the declaration order:

1. Variables
2. Conditions
3. Cursors
4. Handlers
5. DGTTs
Actually DGTT have nothing to do with it. They are just regular
statements which have to come after all the "real" DECLAREs

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 19 '07 #4
Interesting.... as I have been:

variable declarations

cursor declaration...
open cursor
code to handle data coming back....

another cursor declaration...
open cursor
code to handle data coming back...

..and so forth MANY times....

and am now going back to put in some error handling......
so do I have to major reorganize this thing to add a handler for sql
exceptions?

thanks,

Phil Jackson
On Apr 18, 8:41 pm, Serge Rielau <srie...@ca.ibm .comwrote:
jefftyzzer wrote:
On Apr 18, 2:39 pm, PJack...@txfb-ins.com wrote:
Trying to declare a simple handler for SQLEXCEPTION.
Simple stored proc (UDB 8.2)
----
CREATE PROCEDURE TFBUDB.SMACF_SM _DI (IN POL_NBR CHAR(10)
, OUT ErrNo INTEGER
, OUT ErrMsg CHAR(80) )
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
P1: BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE NOTFOUND INTEGER DEFAULT 100;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
SET ErrMsg = 'Houston, we have a problem';
DECLARE D1 CURSOR WITH RETURN TO CALLER FOR
SELECT DI_NME_TXT
,DI_OCCR_NBR
FROM TFBUDB.SM_DI_NM E_INFO
WHERE POL_NBR_TXT = POL_NBR;
OPEN D1;
END P1
--------------------------------------------------------
Attempting to build in DB2 Development center I get the error below -
it certainly does not like it. Syntax for handler right out of book:
TFBUDB.SMACF_SM _DI - Build started.
DROP SPECIFIC PROCEDURE TFBUDB.SQL07041 3170923800
TFBUDB.SMACF_SM _DI - Drop stored procedure completed.
Create stored procedure returns -104.
TFBUDB.SMACF_SM _DI: 20: [IBM][CLI Driver][DB2/NT] SQL0104N An
unexpected token "<cursor declaration>" was found following "".
Expected tokens may include: "<SQL statement>". LINE NUMBER=20.
SQLSTATE=42601
TFBUDB.SMACF_SM _DI - Build failed.
TFBUDB.SMACF_SM _DI - Roll back completed successfully.
OK--back at my desk. Here's the declaration order:
1. Variables
2. Conditions
3. Cursors
4. Handlers
5. DGTTs

Actually DGTT have nothing to do with it. They are just regular
statements which have to come after all the "real" DECLAREs

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

Apr 19 '07 #5
PJ******@txfb-ins.com wrote:
Interesting.... as I have been:

variable declarations
BEGIN
cursor declaration...
open cursor
code to handle data coming back....
END
>
BEGIN
another cursor declaration...
open cursor
code to handle data coming back...
END

... just nest the compounds. Every new BEGINning starts again.

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 19 '07 #6
Fascinating...
So I could have:
variable declarations

BEGIN
cursor declaration...
error handling code here
open cursor
code to handle data coming back....
END

BEGIN
another cursor declaration...
error handling code here
open cursor
code to handle data coming back...

I am up to about a 1,000 line main stored procedure that is calling
about 14-15 other stored procedures, which all are like the fairly
simple one I posted earlier - I wrapped the main stored proc within an
Atomic, Most of these simply declare a cursor for some select...SQL
and return leaving the cursor open. One procedure is called
afterwards each time, after some processing in the main SP, to insert
a row into a table. This happens many, many times. I think the main
SP being Atomic is going to rolll back every thing ok if there is any
failure along the way, I just need to report back specifics to the
client application - what part failed, why it failed, and the specific
insurance policy it failed on. Guessing that maybe this is pushing
the limit of what a stored procedure should be doing? Trying to get a
handle of the architecture of such things in SQL PL...

Thanks for all the good advice to everyone,

Phil Jackson
Apr 19 '07 #7
No problem, not sure if you have experience with other modern languages
like e.g. Java.
It is generally considered good practice to have condition handlers
scoped to only the block that they are meant to handle.
The same can be said for local variables.
All this was incorporated into the SQL/PSM standard which DB2 implements.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 19 '07 #8
On Apr 18, 6:41 pm, Serge Rielau <srie...@ca.ibm .comwrote:
jefftyzzer wrote:
On Apr 18, 2:39 pm, PJack...@txfb-ins.com wrote:
Trying to declare a simple handler for SQLEXCEPTION.
Simple stored proc (UDB 8.2)
----
CREATE PROCEDURE TFBUDB.SMACF_SM _DI (IN POL_NBR CHAR(10)
, OUT ErrNo INTEGER
, OUT ErrMsg CHAR(80) )
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
P1: BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE NOTFOUND INTEGER DEFAULT 100;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
SET ErrMsg = 'Houston, we have a problem';
DECLARE D1 CURSOR WITH RETURN TO CALLER FOR
SELECT DI_NME_TXT
,DI_OCCR_NBR
FROM TFBUDB.SM_DI_NM E_INFO
WHERE POL_NBR_TXT = POL_NBR;
OPEN D1;
END P1
--------------------------------------------------------
Attempting to build in DB2 Development center I get the error below -
it certainly does not like it. Syntax for handler right out of book:
TFBUDB.SMACF_SM _DI - Build started.
DROP SPECIFIC PROCEDURE TFBUDB.SQL07041 3170923800
TFBUDB.SMACF_SM _DI - Drop stored procedure completed.
Create stored procedure returns -104.
TFBUDB.SMACF_SM _DI: 20: [IBM][CLI Driver][DB2/NT] SQL0104N An
unexpected token "<cursor declaration>" was found following "".
Expected tokens may include: "<SQL statement>". LINE NUMBER=20.
SQLSTATE=42601
TFBUDB.SMACF_SM _DI - Build failed.
TFBUDB.SMACF_SM _DI - Roll back completed successfully.
OK--back at my desk. Here's the declaration order:
1. Variables
2. Conditions
3. Cursors
4. Handlers
5. DGTTs

Actually DGTT have nothing to do with it. They are just regular
statements which have to come after all the "real" DECLAREs

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab- Hide quoted text -

- Show quoted text -
I see; good to know--thanks!

--Jeff

Apr 19 '07 #9
Yes, I've been programming in things like VB,ASP,PHP, etc. for years.
This is my first attempt at doing anything other than a simple
declare cursor for
...some sql...
open cursor....

in SQL PL. Still trying to get a handle on how a more complex stored
procedure should be structured. All the examples I've seen are just
pretty much linear, fall through from top to bottom with a few loops
and such interspersed.

thanks!

Phil Jackson
On Apr 19, 6:57 am, Serge Rielau <srie...@ca.ibm .comwrote:
No problem, not sure if you have experience with other modern languages
like e.g. Java.
It is generally considered good practice to have condition handlers
scoped to only the block that they are meant to handle.
The same can be said for local variables.
All this was incorporated into the SQL/PSM standard which DB2 implements.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

Apr 20 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
28076
by: June Moore | last post by:
Hi all, I have a stored procedure that return a resultset e.g. stored proc: get_employee_details select emp_id, emp_name, emp_salary, emp_position from empoloyee I would like to write another stored procedure that executes the above stored procedure - returning the same number of records but it will only show 2 columns
9
6444
by: Wolfgang Kreuzer | last post by:
Try hard to become familiar with T-SQL. Can anybodey tell me the best way to deal with set's provided by a stored procedure. Til yesterday I thougt trapping set in temp table using INSERT EXEC is a way out of this, but then I struggeled with nested INSERT EXEC's. What are all the system proc's good for if the results cannot be evaluated? The approach of modular programming is to have code doing similar things in one place.
0
7152
by: Dave Sisk | last post by:
I've created a system or external trigger on an AS/400 file a.k.a DB2 table. (Note this is an external trigger defined with the ADDPFTRG CL command, not a SQL trigger defined with the CREATE TRIGGER statement.) I've also defined a SQL stored proc, and the trigger is set to call this SP. I've posted the simplified source below. I can manually call the stored proc, and the external trigger is created without any errors. However, when I do...
3
6392
by: vj | last post by:
Please let me know how can i handle any kind of error conditions with a single (condition and continue handler). I need to catch any kind of exception thrown in the procedure with a single ( declare condition and a declare continue handler) Thanks in advance. Vijay
14
1847
by: Roy | last post by:
Apologies for the cross-post, but this truly is a two-sided question. Given the option of creating Looping statements within a stored proc of sql server or in the code-behind of an .net webpage, which would you choose and why? Reason I ask is I created a webpage which essentially runs through a litany of loops to determine which stored proc to kick off. This is written in the code-behind. It occurred to me that I could probably just...
1
1223
by: cleaner7777 | last post by:
Just wondering... When creating triggers, you're allowed to create conditions with the: DECLARE condition-name CONDITION FOR SQLSTATE string-constatnt However, you are not allowed to create handlers in the triggers themselves, but need to call a stored procedure. So... what is the purpose of being able to declare conditions in triggers if you can't use them?
0
2070
by: balaji krishna | last post by:
Hi, I need to handle the return set from COBOL stored procedure from my invoking Java program. I do not know, how many rows the stored proc SQL fetches.I have declared the cursor in that proc, but i don't know how to return the rows the cursor has opened and I don't know how to handle the return set from the proc in my java code. My main problem with that proc is that whether I can retun the result set from the proc without closing the cursor...
5
8849
by: Rahul B | last post by:
Hi, I have very little knowledge about creating Procedures/functions in DB2. When i tried to create the test function like CREATE FUNCTION GET_TEST (P_TEST_ID INTEGER, P_SEL_OR_SORT INTEGER,
0
1996
by: mirandacascade | last post by:
Questions toward the bottom of the post. Situation is this: 1) Access 97 2) SQL Server 2000 3) The Access app: a) sets up pass-thru query b) .SQL property of querydef is a string, the contents of which comprise the call to a stored proc
0
9786
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11125
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10836
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10407
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7962
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4607
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4211
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.