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 

form an sql query using SORT
Goto page 1, 2  Next
 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Utilities
View previous topic :: View next topic  
Author Message
yadav2005
Intermediate


Joined: 10 Jan 2005
Posts: 348
Topics: 144

PostPosted: Thu May 03, 2007 7:59 am    Post subject: form an sql query using SORT Reply with quote

I have a requirement to form a SQL query in the way below which i will use in a JCL :
Code:

SELECT EMPNO,EMPNAME,SALARY
  FROM EMPLOYEE
  WHERE DATE OF JOINING = JOIN DATE (from list)
    AND DEPT            = DEPT NAME (from list)

Explanation somewhat like this:
Code:

SELECT EMPNO,EMPNAME,SALARY
  FROM EMPLOYEE
  WHERE DATE OF JOINING IN ('2007-01-01','2007-02-01','2007-03-01' AND COULD BE OTHERS)
    AND DEPT NAME       IN ('ABC','DEF','GHI','XXX','YYY' AND COULD BE OTHERS)

But i have job flow as below:
Code:

//STEP1 EXEC COBOL PROGRAM WHICH WILL CREATE AN OUTPUT FILE 80 BYTES ASSUMPTION

2007-01-012007-02-012007-03-01....... BASED ON AN INPUT FILE READ FROM OTHER SOURCE
ABCDEFGHIXXXYYY...................... BASED ON AN INPUT FILE READ FROM OTHER SOURCE

//STEP2 EXEC PGM=IKJEFT01
.
.
SELECT EMPNO,EMPNAME,SALARY
  FROM EMPLOYEE
  WHERE DATE OF JOINING = JOIN DATE (from list)
    AND DEPT            = DEPT NAME (from list)

STEP2 should make use of that output file and form the SQL Query .How can i do that , can it be possible through SORT / ICETOOL or any other ways ? Please help me in this regard.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12380
Topics: 75
Location: San Jose

PostPosted: Thu May 03, 2007 8:26 am    Post subject: Reply with quote

yadav2005,

yes it can be done, but a few questions.

1.Does your cobol program write out just 2 records or can it have more than that? Remember that a single sql statement can be a max of 32760 bytes. So if your list is huge then your sql will fail even though sort was able to generate the sql.

2. The first line from your cobol pgm is for dates and 2nd line is for dept?

IMHO the Cobol pgm itself can generate the sql.

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Nic Clouston
Advanced


Joined: 01 Feb 2007
Posts: 1075
Topics: 7
Location: At Home

PostPosted: Thu May 03, 2007 8:28 am    Post subject: Reply with quote

Quote:

I have a requirement to form a SQL query in the way below which i will use in a JCL

JCL cannot execute SQL queries - it tells the m/f what resources the program needs. Please get your terminology right Confused

Once SORT, or whatever, has created the queries how do you propose to execute them? Generally queries are assembled and executed within programs (as opposed to utilities).
_________________
Utility and Program control cards are NOT, repeat NOT, JCL.
Back to top
View user's profile Send private message
yadav2005
Intermediate


Joined: 10 Jan 2005
Posts: 348
Topics: 144

PostPosted: Thu May 03, 2007 8:51 am    Post subject: Reply with quote

Kolusu,

Thanks for your replies.

1.Does your cobol program write out just 2 records or can it have more than that? Remember that a single sql statement can be a max of 32760 bytes. So if your list is huge then your sql will fail even though sort was able to generate the sql.

Ans: It will write only 2 records with 1 st record with all dates one after the other and 2 nd record will have deptartment names one after the other.Because the program will create the file and it wont's be that long enough ,how can we handle the maximum bytes concept of 32760.Can this part be taken care in the program / SORT ?

2. The first line from your cobol pgm is for dates and 2nd line is for dept?

Ans: The first line is for Dates and second line is for Dept.

How can the Cobol Program itself generate the SQL ? Can you help in both the solutions ?
Back to top
View user's profile Send private message
ramlak
Beginner


Joined: 17 Apr 2007
Posts: 27
Topics: 6

PostPosted: Thu May 03, 2007 9:02 am    Post subject: Reply with quote

yadav,

The easiest way would be generating the SQL in a flat file by the cobol program and using that you can execute the query in the JCL.

Read the dates generated delimited by comma and move it to the group variable which has the value 'SELECT .....'

and finally write the query to a file...

Jus an idea may be wrong...

Regards,
Lak.
Back to top
View user's profile Send private message
Nic Clouston
Advanced


Joined: 01 Feb 2007
Posts: 1075
Topics: 7
Location: At Home

PostPosted: Thu May 03, 2007 9:13 am    Post subject: Reply with quote

JCL cannot execute SQL queries.

You execute within a program, or SPUFI or QMF.
Within a program e.g.

Code:
EXEC SQL
    sql statement
END SQL.


It is in the manual - one of the first things you learn.
_________________
Utility and Program control cards are NOT, repeat NOT, JCL.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12380
Topics: 75
Location: San Jose

PostPosted: Thu May 03, 2007 9:24 am    Post subject: Reply with quote

yadav2005,

Here is a sort job which will generate the sql.

Code:

//STEP0100 EXEC PGM=SORT                               
//SYSOUT   DD SYSOUT=*                                 
//SORTIN   DD *                                       
2007-01-012007-02-012007-03-01                         
ABCDEFGHIXXXYYY......................                 
//SORTOUT  DD SYSOUT=*                                 
//SYSIN    DD *                                       
  SORT FIELDS=COPY                                     
  OUTREC FIELDS=(01,80,SEQNUM,1,ZD)                   
  OUTFIL ENDREC=2,IFOUTLEN=80,                         
         IFTHEN=(WHEN=(81,1,ZD,EQ,1),                 
         BUILD=(C'SELECT EMPNO,EMPNAME,SALARY',/,     
                C'  FROM EMPLOYEE',/,                 
                C' WHERE JOINDATE IN (',               
                C'''',01,10,C'''',/,                   
                19X,C',',C'''',11,10,C'''',/,         
                19X,C',',C'''',21,10,C'''',/,         
                19X,C',',C'''',31,10,C'''',/,         
                19X,C',',C'''',41,10,C'''',/,         
                19X,C',',C'''',51,10,C'''',/,         
                19X,C',',C'''',61,10,C'''',/,         
                19X,C',',C'''',71,10,C'''',C')',80:X)),
                                                       
         IFTHEN=(WHEN=(81,1,ZD,EQ,2),                 
         BUILD=(C'   AND DEPT IN (',                   
                C'''',01,03,C'''',/,                   
                15X,C',',C'''',04,03,C'''',/,         
                15X,C',',C'''',07,03,C'''',/,         
                15X,C',',C'''',10,03,C'''',/,         
                15X,C',',C'''',13,03,C'''',/,         
                15X,C',',C'''',16,03,C'''',/,         
                15X,C',',C'''',19,03,C'''',/,         
                15X,C',',C'''',22,03,C'''',/,         
                15X,C',',C'''',25,03,C'''',/,         
                15X,C',',C'''',28,03,C'''',/,         
                15X,C',',C'''',31,03,C'''',/,         
                15X,C',',C'''',34,03,C'''',/,         
                15X,C',',C'''',37,03,C'''',/,         
                15X,C',',C'''',40,03,C'''',/,         
                15X,C',',C'''',43,03,C'''',/,         
                15X,C',',C'''',46,03,C'''',/,         
                15X,C',',C'''',49,03,C'''',/,         
                15X,C',',C'''',52,03,C'''',/,         
                15X,C',',C'''',55,03,C'''',/,         
                15X,C',',C'''',58,03,C'''',/,         
                15X,C',',C'''',61,03,C'''',/,         
                15X,C',',C'''',64,03,C'''',/,         
                15X,C',',C'''',67,03,C'''',/,         
                15X,C',',C'''',70,03,C'''',/,         
                15X,C',',C'''',73,03,C'''',/,         
                15X,C',',C'''',76,03,C'''',C');',80:X))
/*


Quote:
How can the Cobol Program itself generate the SQL ? Can you help in both the solutions ?


As for the cobol solution that should be walk in the park. Just define another file and write out the sql to that file. It is as simple as that.

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu


Last edited by kolusu on Thu May 03, 2007 9:29 am; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12380
Topics: 75
Location: San Jose

PostPosted: Thu May 03, 2007 9:28 am    Post subject: Reply with quote

Nic Clouston wrote:
JCL cannot execute SQL queries.

You execute within a program, or SPUFI or QMF.
Within a program e.g.

Code:
EXEC SQL
    sql statement
END SQL.


It is in the manual - one of the first things you learn.


Nic,

I guess the whole point of generating the sql is to run in batch thru utilities like DNSTIAUL or DSNTEP2. Sad but true many of the newbie programmers think any utility which can invoked in a JCL is considered "A super JCL package which can literally do anything". I tried to educate but gave up Crying or Very sad Crying or Very sad

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
yadav2005
Intermediate


Joined: 10 Jan 2005
Posts: 348
Topics: 144

PostPosted: Thu May 03, 2007 9:41 am    Post subject: Reply with quote

Thanks Kolusu,

The solution provided by you worked fine and i am able to understand your logic.Thank you.
Back to top
View user's profile Send private message
Nic Clouston
Advanced


Joined: 01 Feb 2007
Posts: 1075
Topics: 7
Location: At Home

PostPosted: Thu May 03, 2007 10:36 am    Post subject: Reply with quote

Kolusu,

I won't give up! Sloppiness or misunderstanding at this level could mean getting something seriously wrong later or reduce their job prospects. I would not hire someone who did not SEEM to understand what JCL is when it is an everyday part of their job.
_________________
Utility and Program control cards are NOT, repeat NOT, JCL.
Back to top
View user's profile Send private message
yadav2005
Intermediate


Joined: 10 Jan 2005
Posts: 348
Topics: 144

PostPosted: Mon Nov 12, 2007 8:29 am    Post subject: Reply with quote

Kolusu,

Thanks a lot for your post.I executed your code and i got the output as:
Code:

SELECT EMPNO,EMPNAME,SALARY                                                     
  FROM EMPLOYEE                                                                 
 WHERE JOINDATE IN ('2007-01-01'                                               
                   ,'2007-02-01'                                               
                   ,'2007-03-01'                                               
                   ,'          '                                               
                   ,'          '                                               
                   ,'          '                                               
                   ,'          '                                               
                   ,'          ')                                               
   AND DEPT IN ('ABC'                                                           
               ,'DEF'                                                           
               ,'GHI'                                                           
               ,'XXX'                                                           
               ,'YYY'                                                           
               ,'...'                                                           
              ,'...'                                                           
              ,'...'                                                           
              ,'...'                                                           
              ,'...'                                                           
              ,'...'                                                           
              ,'...'                                                           
              ,'.  '                                                           
              ,'   '                                                           
              ,'   '                                                           
              ,'   '                                                           
              ,'   '                                                           
              ,'   '                                                           
              ,'   '                                                           
              ,'   '                                                           
              ,'   '                                                           
              ,'   '                                                           
              ,'   '                                                           
               ,'   '                   
               ,'   '                   

Is there any way to stop the code after the last Date of joining as well as last dept so that in the IN List we do not get the empty date of joining and dept and i am looking for the query as below:
Code:

SELECT EMPNO,EMPNAME,SALARY     
  FROM EMPLOYEE                 
 WHERE JOINDATE IN ('2007-01-01'
                   ,'2007-02-01'
                   ,'2007-03-01')
   AND DEPT IN ('ABC'
               ,'DEF'
               ,'GHI'
               ,'XXX'
               ,'YYY');
Back to top
View user's profile Send private message
vkphani
Intermediate


Joined: 05 Sep 2003
Posts: 483
Topics: 48

PostPosted: Mon Nov 12, 2007 10:12 am    Post subject: Reply with quote

yadav2005,

The below code will give you the desired results.
Code:
//STEP0100 EXEC PGM=SORT                               
//SYSOUT   DD SYSOUT=*                                 
//SORTIN   DD *                                         
2007-01-012007-02-012007-03-01                         
ABCDEFGHIXXXYYY......................                   
//SORTOUT  DD SYSOUT=*                                 
//SYSIN    DD *                                         
  SORT FIELDS=COPY                                     
  OUTREC FIELDS=(01,80,SEQNUM,1,ZD)                     
  OUTFIL ENDREC=2,IFOUTLEN=80,                         
         IFTHEN=(WHEN=(81,1,ZD,EQ,1),                   
         BUILD=(C'SELECT EMPNO,EMPNAME,SALARY',/,       
                C'  FROM EMPLOYEE',/,                   
                C' WHERE JOINDATE IN (',               
                C'''',01,10,C'''',/,                   
                19X,C',',C'''',11,10,C'''',/,           
                19X,C',',C'''',21,10,C'''',C')',80:X)),
        IFTHEN=(WHEN=(81,1,ZD,EQ,2),                   
         BUILD=(C'   AND DEPT IN (',                   
                C'''',01,03,C'''',/,                   
                15X,C',',C'''',04,03,C'''',/,           
                15X,C',',C'''',07,03,C'''',/,           
                15X,C',',C'''',10,03,C'''',/,           
                15X,C',',C'''',13,03,C'''',C');',80:X))
/*                                                     

SORTOUT:
Code:
SELECT EMPNO,EMPNAME,SALARY     
  FROM EMPLOYEE                 
 WHERE JOINDATE IN ('2007-01-01'
                   ,'2007-02-01'
                   ,'2007-03-01')
   AND DEPT IN ('ABC'           
               ,'DEF'           
               ,'GHI'           
               ,'XXX'           
               ,'YYY');         
Back to top
View user's profile Send private message Send e-mail
yadav2005
Intermediate


Joined: 10 Jan 2005
Posts: 348
Topics: 144

PostPosted: Mon Nov 12, 2007 11:13 am    Post subject: Reply with quote

vkphani,

Thanks for your reply.Your solution will work if u know the fixed number of Date of joining and Code and in this case u have three date of joinings and 5 codes and you have coded as per it.In my case i do not know how many Date of joinings i will have in that 1 st record but maximum lrecl = 80 bytes and how many codes i will have in the second record is also unkown.If you see kolusu post , i do not want to form the query with spaces.My requirement is to form a query somehow and i guess we should have some indication of end of the line.

Please let me know how it can be done.Thanks.
Back to top
View user's profile Send private message
yadav2005
Intermediate


Joined: 10 Jan 2005
Posts: 348
Topics: 144

PostPosted: Mon Nov 12, 2007 12:21 pm    Post subject: Reply with quote

Members,

Can this solution be done by executing any REXX Exec , i do not have any knowledge on REXX , can somebody please help me out in this requirement. I guess with some minor modifications to Kolusu's SORT code , the query can be generated.

Thanks to all for the support.
Back to top
View user's profile Send private message
Frank Yaeger
Sort Forum Moderator
Sort Forum Moderator


Joined: 02 Dec 2002
Posts: 1618
Topics: 31
Location: San Jose

PostPosted: Mon Nov 12, 2007 3:29 pm    Post subject: Reply with quote

Here's a DFSORT/ICETOOL job that will do what you asked for:

Code:

//S1    EXEC  PGM=ICETOOL
//TOOLMSG DD SYSOUT=*
//DFSMSG  DD SYSOUT=*
//IN DD *
2007-01-012007-02-012007-03-01
ABCDEFGHIXXXYYY
/*
//T1 DD DSN=&&T1,UNIT=SYSDA,SPACE=(CYL,(5,5)),DISP=(,PASS)
//OUT DD SYSOUT=*
//TOOLIN DD *
COPY FROM(IN) USING(CTL1)
COPY FROM(T1) USING(CTL2)
/*
//CTL1CNTL DD *
  INREC OVERLAY=(81:SEQNUM,1,ZD)
  OUTFIL FNAMES=T1,
    IFTHEN=(WHEN=(81,1,ZD,EQ,+1),
      BUILD=(5:C'''',1,10,C'''',31:C'1',/,
             4:C',''',11,10,C'''',/,
             4:C',''',21,10,C'''',/,
             4:C',''',31,10,C'''',/,
             4:C',''',41,10,C'''',/,
             4:C',''',51,10,C'''',/,
             4:C',''',61,10,C'''',/,
             4:C',''',71,10,C'''')),
    IFTHEN=(WHEN=(81,1,ZD,EQ,+2),
      BUILD=(5:C'''',1,3,C'''',31:C'2',/,
             4:C',''',4,3,C'''',/,
             4:C',''',7,3,C'''',/,
             4:C',''',10,3,C'''',/,
             4:C',''',13,3,C'''',/,
             4:C',''',16,3,C'''',/,
             4:C',''',19,3,C'''',/,
             4:C',''',22,3,C'''',/,
             4:C',''',25,3,C'''',/,
             4:C',''',28,3,C'''',/,
             4:C',''',31,3,C'''',/,
             4:C',''',34,3,C'''',/,
             4:C',''',37,3,C'''',/,
             4:C',''',40,3,C'''',/,
             4:C',''',43,3,C'''',/,
             4:C',''',46,3,C'''',/,
             4:C',''',49,3,C'''',/,
             4:C',''',52,3,C'''',/,
             4:C',''',55,3,C'''',/,
             4:C',''',58,3,C'''',/,
             4:C',''',61,3,C'''',/,
             4:C',''',64,3,C'''',/,
             4:C',''',67,3,C'''',/,
             4:C',''',70,3,C'''',/,
             4:C',''',73,3,C'''',/,
             4:C',''',76,3,C''''))
/*
//CTL2CNTL DD *
  OMIT COND=(6,3,CH,EQ,C' ')
  OUTFIL FNAMES=OUT,REMOVECC,
    IFOUTLEN=80,
    IFTHEN=(WHEN=(31,1,CH,EQ,C'1'),
      BUILD=(C'SELECT EMPNO,EMPNAME,SALARY',/,
             C'  FROM EMPLOYEE',/,
             C' WHERE JOINDATE IN (',/,
             1,30)),
    IFTHEN=(WHEN=(31,1,CH,EQ,C'2'),
      BUILD=(C'  )',/,
             C'   AND DEPT IN (',/,
             1,30)),
    TRAILER1=('  );')
/*


For this example, OUT would have:

Code:

SELECT EMPNO,EMPNAME,SALARY       
  FROM EMPLOYEE                   
 WHERE JOINDATE IN (               
    '2007-01-01'                   
   ,'2007-02-01'                   
   ,'2007-03-01'                   
  )                               
   AND DEPT IN (                   
    'ABC'                         
   ,'DEF'                         
   ,'GHI'                         
   ,'XXX'                         
   ,'YYY'                         
  );                               

_________________
Frank Yaeger - DFSORT Development Team (IBM)
Specialties: JOINKEYS, FINDREP, WHEN=GROUP, ICETOOL, Symbols, Migration
DFSORT is on the Web at:
www.ibm.com/storage/dfsort
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Utilities All times are GMT - 5 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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