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 > How to write if logic in store procedure?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-14-07, 04:22
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
How to write if logic in store procedure?

Hi,
on db2 v8.2 FP9 on Windows XP I would like to write a store procedure which will declare cursor according to input parameter using if logic.

Sample:
Code:
CREATE PROCEDURE ADMIN.TEST (
     IN InParameter CHAR(1)    )

P1: BEGIN

IF InParameter = '1' THEN
     DECLARE cursor1 CURSOR WITH RETURN FOR SELECT 'A' FROM SYSIBM.SYSDUMMY1;
ELSE
     DECLARE cursor1 CURSOR WITH RETURN FOR SELECT 'B' FROM SYSIBM.SYSDUMMY1;
END IF;     
  
  OPEN cursor1;
END P1    
@
Note: Select statements in above sample are simplified samples. In my case there are two different select statements.

I get error:
================================================== =====================
C:\aaa>DB2 -TD@ -F A.SQL
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0104N An unexpected token "<cursor declaration>" was found following "".
Expected tokens may include: "<SQL statement>". LINE NUMBER=7.
SQLSTATE=42601
================================================== =====================

It looks like there is some problem using if and declare cursor. Any idea what is wrong with my procedure?

Thanks,
Grofaty
Reply With Quote
  #2 (permalink)  
Old 09-14-07, 04:39
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Code:
IF InParameter = '1' BEGIN
  ...
END
ELSE BEGIN
 ...
END
?
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 09-14-07, 04:59
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,
thanks for help.

I wrote it this way and it works fine:
Code:
CREATE PROCEDURE ADMIN.TEST (
     IN InParameter CHAR(1)    )

P1: BEGIN
     DECLARE cursor1 CURSOR WITH RETURN FOR SELECT 'A' FROM SYSIBM.SYSDUMMY1;
     DECLARE cursor2 CURSOR WITH RETURN FOR SELECT 'B' FROM SYSIBM.SYSDUMMY1;

     IF InParameter = '1' THEN  
       OPEN cursor1;
     ELSE
       OPEN cursor2;
     END IF;       

END P1    
@
Thanks,
Grofaty
Reply With Quote
  #4 (permalink)  
Old 09-15-07, 09:39
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
That is fine.

DECLARE statements (for variables, cursors, and condition handlers) must come first in a BEGIN ... END block (compound statement). So either you nest a new compound statement in the branches, or you do what you did.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #5 (permalink)  
Old 09-15-07, 10:52
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Quote:
Originally Posted by grofaty
Hi,
thanks for help.

I wrote it this way and it works fine:
Code:
CREATE PROCEDURE ADMIN.TEST (
     IN InParameter CHAR(1)    )

P1: BEGIN
     DECLARE cursor1 CURSOR WITH RETURN FOR SELECT 'A' FROM SYSIBM.SYSDUMMY1;
     DECLARE cursor2 CURSOR WITH RETURN FOR SELECT 'B' FROM SYSIBM.SYSDUMMY1;

     IF InParameter = '1' THEN  
       OPEN cursor1;
     ELSE
       OPEN cursor2;
     END IF;       

END P1    
@
Thanks,
Grofaty
This is fine becasue a DECLARE CURSOR does not execute any procedural code until it is opened.

In fact, when using programming languages like COBOL, the DECLARE CURSOR is usually included in the WORKING STORAGE SECTION and not in the PROCEDURE DIVISION.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
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