View previous topic :: View next topic |
Author |
Message |
mnandakumar Beginner
Joined: 09 May 2007 Posts: 22 Topics: 14
|
Posted: Wed Oct 10, 2007 5:57 am Post subject: Dynamic IN Clause in sql |
|
|
Suggest me a way in COBOL to add dynamically values to IN Clause in sql.
Example :
During one run of the program the IN Claouse in sql may look like this
IN('IPC1','IPC2','IPC3')
this may change as
IN('IPC1','IPC2','IPC3','IPC4','IPC5')
Dynamic sql should not be used.
Please suggest me a way
Advance thannks. |
|
Back to top |
|
 |
vivek1983 Intermediate

Joined: 20 Apr 2006 Posts: 222 Topics: 24
|
Posted: Wed Oct 10, 2007 6:27 am Post subject: |
|
|
mnandakumar,
Can you tell me how those values comes in the first place? Are the values from any file? How many values maximum is expected in the IN clause? _________________ Vivek G
--------------------------------------
A dream is just a dream. A goal is a dream with a plan and a deadline. (Harvey Mackay) |
|
Back to top |
|
 |
sriramla Beginner
Joined: 22 Feb 2003 Posts: 74 Topics: 1
|
Posted: Wed Oct 10, 2007 9:22 am Post subject: |
|
|
You can try the following couple of suggestions:
1. If the IN clause values are always going to be sequential as given in your example (IPC1 thru IPC3 or IPC1 thru IPC5) then replace the IN clause with BETWEEN clause.
2. If they are not always sequential then identify the maximum number of values you expect. Lets say its 10. Then have 10 host variables in your query and value them appropriately. Note that you need to initialize the remaining host variables to a non-existing value in the database to avoid pulling incorrect records from the query. |
|
Back to top |
|
 |
jsharon1248 Intermediate
Joined: 08 Aug 2007 Posts: 291 Topics: 2 Location: Chicago
|
Posted: Wed Oct 10, 2007 1:10 pm Post subject: |
|
|
A different option is to utilize a declared temporary table. There are no special authorizations required to create a declared temporary table. Create the table in the COBOL program, insert the values into the table, and modify your SQL to join to the table. Click on the Quick Manuals link at the top of all the pages in this forum, open the DB2 UDB for Z/OS SQL Reference and review the DECLARE GLOBAL TEMPORARY TABLE statement for more details. For 5 or 10 items, you'd probably want to use host variables, but if you have more than that or just plain don't know, the declared temporary table is a good option. |
|
Back to top |
|
 |
jsharon1248 Intermediate
Joined: 08 Aug 2007 Posts: 291 Topics: 2 Location: Chicago
|
Posted: Wed Oct 10, 2007 1:30 pm Post subject: |
|
|
Clarification: for your purposes, code the IN predicate with a non-correlated sub-query to obtain the list.
Code: |
EXEC SQL
DECLARE CSR1 CURSOR FOR
SELECT <col-list>
FROM ORIG_TBL
WHERE COLA IN (SELECT IN_COL1 FROM SESSION.IN_LIST_TBL)
END-EXEC
EXEC SQL
DECLARE GLOBAL TEMPORARY TABLE IN_LIST_TBL
(IN_COL1 CHAR(15))
ON COMMIT PRESERVE ROWS
END-EXEC
PERFORM UNTIL <no more values>
INSERT INTO SESSION.IN_LIST_TBL IN_COL1 VALUES (:HV)
END-PERFORM
<process cursor>
|
If I remember correctly, you'll need to use the VALIDATE(RUN) option when you BIND the package. |
|
Back to top |
|
 |
|
|