IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Dynamic SQL for Fixed list result but variable WHERE clause.


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
abdul.faras

New User


Joined: 29 Feb 2008
Posts: 49
Location: Pune

PostPosted: Tue Feb 15, 2011 1:38 pm
Reply with quote

I need to code a dynamic SQL which will fetch a fixed number of columns (so fixed list), but the where clause may vary on the basis or requirement.

What I found in Redbook is this:
Code:
EXEC SQL DECLARE C1 CURSOR FOR STMT;
MOVE 'SELECT NAME, PHONE, ADDRESS, STATE FROM TABLE1 WHERE PHONE = ?' TO DSTRING
EXEC SQL PREPARE STMT FROM :DSTRING;
EXEC SQL OPEN C1 USING :PARM1;
EXEC SQL FETCH C1 INTO :NAME, :PHONE, :ADDRESS, :STATE;


But here I believe the constrain is PARM1 should match with 1st field in the where clause.... & so on...

My requirement is:
I should be able to build the where clause dynamically. For example:
Case 1. Need to pass PHONE in WHERE clause.
Case 2. Need to pass NAME in WHERE clause.

Could you help me in coding this.
Or shall I refer to the VARYING-LIST Dynamic SQL.[/code]
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Tue Feb 15, 2011 2:10 pm
Reply with quote

Code:
if blabla
   MOVE 'SELECT NAME, PHONE, ADDRESS, STATE FROM TABLE1 WHERE PHONE = ?' TO DSTRING
   EXEC SQL PREPARE STMT FROM :DSTRING;
   EXEC SQL OPEN C1 USING :PARM-phone;
else
   MOVE 'SELECT NAME, PHONE, ADDRESS, STATE FROM TABLE1 WHERE NAME = ?' TO DSTRING
   EXEC SQL PREPARE STMT FROM :DSTRING;
   EXEC SQL OPEN C1 USING :parm-name;
end-if
edited
Back to top
View user's profile Send private message
grayWolf

New User


Joined: 04 Oct 2010
Posts: 19
Location: Land of broken dreams

PostPosted: Thu Feb 17, 2011 12:59 pm
Reply with quote

Considering a dynamic query, a better approach would be using something like this:

In the working storage declare as below

Code:
01  WS-QUERY1.
  05 WS-QUERY1A.
     10 PIC X(08) VALUE ' SELECT '.
     10 PIC X(5) VALUE   ' NAME,'.
     10 PIC X(06) VALUE   ' PHONE,'.
     10 PIC X(10) VALUE   ' ADDRESS,'.
     10 PIC X(5) VALUE   ' STATE ,'.
     10 PIC X(12) VALUE   ' FROM TABLE1'.
  05 WS-WHERE PIC X(30) VALUE SPACES.
  05 WS-QUERY1B PIC X(24)  VALUE ' FOR FETCH ONLY WITH UR'.


In the WS-WHERE variable, move whatever "WHERE" clause is required.
(This approach is best suited when multiple conditions have to checked. If you have only 2 cases follow the other approach)

MOVE ws-query1 to DSTRING
Prepare the statement and open the dynamic cursor as given by Guy


Let us know if it worked.


Regards,
Back to top
View user's profile Send private message
abdul.faras

New User


Joined: 29 Feb 2008
Posts: 49
Location: Pune

PostPosted: Fri Feb 18, 2011 1:07 pm
Reply with quote

Thanks GuyC and Wolf..
Will try both..
icon_biggrin.gif
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Store the data for fixed length COBOL Programming 1
No new posts Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
No new posts Pulling a fixed number of records fro... DB2 2
No new posts How to create a list of SAR jobs with... CA Products 3
No new posts Using Dynamic file handler in the Fil... COBOL Programming 2
Search our Forums:

Back to Top