IBM Mainframe Forum Index
 
Register
 
IBM Mainframe Forum Index Mainframe: Search Log in to check your private messages Log in
 

Generate SQL query dynamically using the symbolic parameters


 
IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
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
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: 2481
Location: @my desk

PostPosted: Tue Aug 28, 2018 10:30 pm
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
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: 2431
Location: Hampshire, UK

PostPosted: Wed Aug 29, 2018 1:29 am
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: 2481
Location: @my desk

PostPosted: Wed Aug 29, 2018 2:21 am
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: 446
Location: USA

PostPosted: Wed Aug 29, 2018 2:57 am
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

Global Moderator


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

PostPosted: Wed Aug 29, 2018 3:25 am
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: 2481
Location: @my desk

PostPosted: Wed Aug 29, 2018 7:08 pm
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 All times are GMT + 6 Hours
Forum Index -> DB2
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Forum Replies
No new posts Dynamically switching terminal model TSO/ISPF 9
No new posts XML-GENERATE to render edited numeric COBOL Programming 0
No new posts Dynamically read input files using BP... COBOL Programming 20
No new posts Mainframe ISPREDIT Macro query TSO/ISPF 3
No new posts Read a file dynamically without the d... COBOL Programming 4

Back to Top