Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
Generate SQL query dynamically using the symbolic parameters

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
vnktrrd

New User


Joined: 12 Jan 2010
Posts: 34
Location: New York

PostPosted: Tue Aug 28, 2018 8:11 pm    Post subject: Generate SQL query dynamically using the symbolic parameters
Reply with quote

Hi,

I need help in achieving my requirement.
Thanks in advance.

I receive parms to JCL and based on that I have to prepare a SQL query which is to be run in the next step.

Input parms :

Input1
Input2
Input3

Select * from table1
Where table_field = Input1
and table_field = Input2
and table_field = Input3 ;

depending on what I receive in the Inputs the SQL query should be generated.
If there is no value in Input2 then the query should be like this

Select * from table1
Where table_field = Input1
and table_field = Input3 ;

Thanks,
Ramana.
Back to top
View user's profile Send private message

Arun Raj

Moderator


Joined: 17 Oct 2006
Posts: 2427
Location: @my desk

PostPosted: Tue Aug 28, 2018 10:30 pm    Post subject:
Reply with quote

Quote:
Select * from table1
Where table_field = Input1
and table_field = Input2
and table_field = Input3 ;
Are you sure it is 'and' and not 'or'?
Back to top
View user's profile Send private message
vnktrrd

New User


Joined: 12 Jan 2010
Posts: 34
Location: New York

PostPosted: Wed Aug 29, 2018 1:09 am    Post subject: Reply to: Generate SQL query dynamically using the symbolic parameters
Reply with quote

It can be anything. I just gave an example with 'AND'. Does it make any difference ? All I want is to generate a query using the JCL parms.

Thanks,
Ramana.
Back to top
View user's profile Send private message
Nic Clouston

Global Moderator


Joined: 10 May 2007
Posts: 2110
Location: UK

PostPosted: Wed Aug 29, 2018 1:29 am    Post subject: Reply to: Generate SQL query dynamically using the symbolic parameters
Reply with quote

Write your program then!
Back to top
View user's profile Send private message
Arun Raj

Moderator


Joined: 17 Oct 2006
Posts: 2427
Location: @my desk

PostPosted: Wed Aug 29, 2018 2:21 am    Post subject:
Reply with quote

Quote:
It can be anything. I just gave an example with 'AND'. Does it make any difference ?
Yes it might make a difference in your results. 'AND' does not make much sense in the context you showed. As far as building your sql dynamically, as Nic pointed out, you could choose any programming language of convenience, I'm sure there would be working examples in this forum. Post here if you come across any issues and someone would be around to help you. Good luck.
Back to top
View user's profile Send private message
RahulG31

Active User


Joined: 20 Dec 2014
Posts: 444
Location: USA

PostPosted: Wed Aug 29, 2018 2:57 am    Post subject: Reply to: Generate SQL query dynamically using the symbolic parameters
Reply with quote

If you are looking for any kind of SORT solution then you can probably make a use of PARM JPn. You can write something similar to:
Code:
//STEPSORT EXEC PGM=SORT,PARM='JP1"IN1",JP2"IN2",JP3"   "'
//SYSOUT   DD SYSOUT=*
//SYSPRINT DD SYSOUT=*
//SORTIN   DD *

/*
//SORTOUT DD SYSOUT=*
//SYSIN DD *
 OUTFIL IFTHEN=(WHEN=(1,3,CH,EQ,JP1),
        BUILD=(C'SELECT * FROM TABLE',/,C'WHERE TABLE_FIELD=',JP2,/,
               C'  AND TABLE_FIELD=',JP3)),
        IFTHEN=(WHEN=(1,3,CH,EQ,JP2),
        BUILD=(C'SELECT * FROM TABLE',/,C'WHERE TABLE_FIELD=',JP1,/,
               C'  AND TABLE_FIELD=',JP3)),
        IFTHEN=(WHEN=(1,3,CH,EQ,JP3),
        BUILD=(C'SELECT * FROM TABLE',/,C'WHERE TABLE_FIELD=',JP1,/,
               C'  AND TABLE_FIELD=',JP2))
 OPTION COPY
/*

SORTIN is having a blank line so that we can compare it with JPn values. I assumed all input fields have 3 byte length.

You can manipulate this logic according to your requirement.

.
Back to top
View user's profile Send private message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 2038
Location: NY,USA

PostPosted: Wed Aug 29, 2018 3:25 am    Post subject:
Reply with quote

Instead of convoluted logic simply unload the table to a data set and use DFSORT include to play around The filter based on the input values
Back to top
View user's profile Send private message
Arun Raj

Moderator


Joined: 17 Oct 2006
Posts: 2427
Location: @my desk

PostPosted: Wed Aug 29, 2018 7:08 pm    Post subject: Reply to: Generate SQL query dynamically using the symbolic parameters
Reply with quote

Here is another way of using the sort product (if you are at z/OS 2.1 or higher). Please note, I have included a dummy value at the end.
Code:
// EXPORT SYMLIST=(IN1,IN2,IN3)                       
// SET IN1='AAA',IN2='BBB',IN3='CCC'                 
//STEP01   EXEC  PGM=SORT                             
//SORTIN   DD *,SYMBOLS=JCLONLY                       
&IN1                                                 
&IN2                                                 
&IN3                                                 
//SYSIN    DD *                                       
  INCLUDE COND=(1,3,CH,NE,C' ')                       
  SORT FIELDS=COPY                                   
  OUTFIL REMOVECC,HEADER1=('SELECT * FROM TABLE',/,   
                           'WHERE TABLE_FIELD IN ( '),
                    BUILD=(C'''',1,3,C''',',80:X),   
                 TRAILER1=('''---'')')               
//SYSOUT   DD SYSOUT=*                               
//SORTOUT  DD SYSOUT=*                               
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Query on secondary index Suja.Sai IMS DB/DC 0 Wed Oct 31, 2018 9:47 pm
No new posts Query regarding STOP REGION XX ABDU... ashek15 IMS DB/DC 11 Fri Oct 19, 2018 10:13 am
No new posts Facing issue while executing multi ro... aagarwal88 DB2 6 Tue Oct 02, 2018 8:11 am
No new posts DB2 SQL Query to fetch all instances ... MallikarjunSM DB2 2 Thu Sep 27, 2018 6:46 pm
No new posts What does parameters mean in PL/I? jackzhang75 PL/I & Assembler 6 Thu Aug 30, 2018 8:36 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us