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

Using Dynamic SQL in DB2 2

Status
Not open for further replies.

Dunshee

IS-IT--Management
Feb 1, 2002
6
US
I need to use dynamic SQL in my COBOL program. Environment is DB2 / COBOL 370.

I need to build the actual select statement for the cursor, including a term "LOCATION IN ('xxx', 'xxx',...)
where I'll be getting a random (but small) number of
xxx's at run time. I now have a kludgey set up with
(LOCATION = :WS-XXX-1 OR
LOCATION = :WS-XXX-2 OR
LOCATION = :WS-XXX-3 OR
LOCATION = :WS-XXX-4 OR
LOCATION = :WS-XXX-5 )
which looks dopey and has to be increased when I
need to handle 6 or 7 locations...


I know there is a way to do this, but I can't find any
examples to steal (my favorite way of coding).

If I have to build a work area for the dynamic SQL, how
do I estimate its size?

Thanks much,
-Dunshee/

 
Hi I'm not familiar with 370 Cobol but for the AS 400 you'd probably use the varying-list Dynamic SQL. This is where the number and type of parameters are unknown. Do you have this available in Cobol 370? On the AS 400 each parameter marker is a question mark (?).
 
Hi,

You wrote:

.... but I can't find any
examples to steal (my favorite way of coding).


You broke into the wrong house. Have you tried the DB2 Forum?
 
Hi Dun,
I've posted this in the DB2 forum too, but added here just in case anybody's interested..... What you need to do is to build the SQL dynamically in your program and then execute by issuing a PREPARE statement the format is something along the lines of.....
EXEC SQL
DECLARE CURS1 CURSOR FOR STMT1
EXEC SQL

In the logic of the program
PREPARE STMT1 INTO SQLDA FROM :WS-STATEMENT
END-EXEC
(having previously built your SELECT in WS-STATEMENT)

You'll then need to
EXEC SQL
OPEN CURS1 USING DESCRIPTOR :SQLDA
END-EXEC

and then fetch each row
EXEC SQL
FETCH CURS1 USING DESCRIPTOR :SQLDA
END-EXEC

You'll need to set up the SQLDA area which will contain the dynamic results of the read.

Hope this helps, come back to me if it doesn't

Marc Lodge
 
Thanks, MarcLodge, your example, and that on the link, will help. -dd/
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top