MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Dynamic IN Clause in sql

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
mnandakumar
Beginner


Joined: 09 May 2007
Posts: 22
Topics: 14

PostPosted: Wed Oct 10, 2007 5:57 am    Post subject: Dynamic IN Clause in sql Reply with quote

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
View user's profile Send private message
vivek1983
Intermediate


Joined: 20 Apr 2006
Posts: 222
Topics: 24

PostPosted: Wed Oct 10, 2007 6:27 am    Post subject: Reply with quote

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
View user's profile Send private message
sriramla
Beginner


Joined: 22 Feb 2003
Posts: 74
Topics: 1

PostPosted: Wed Oct 10, 2007 9:22 am    Post subject: Reply with quote

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
View user's profile Send private message
jsharon1248
Intermediate


Joined: 08 Aug 2007
Posts: 291
Topics: 2
Location: Chicago

PostPosted: Wed Oct 10, 2007 1:10 pm    Post subject: Reply with quote

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
View user's profile Send private message
jsharon1248
Intermediate


Joined: 08 Aug 2007
Posts: 291
Topics: 2
Location: Chicago

PostPosted: Wed Oct 10, 2007 1:30 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group