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

case statement in a where clause


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

New User


Joined: 03 Mar 2008
Posts: 32
Location: India

PostPosted: Wed Sep 16, 2009 3:40 pm
Reply with quote

I am trying to use host variables conditionally in a where clause of my program.

the variables i have declared are:

01 WS-REGULAR-VAR PIC X(1).
01 WS-EXCEPTIONS.
05 CODE1 PIC X(1) VALUE 'A'.
05 CODE2 PIC X(1) VALUE 'B'.
05 CODE3 PIC X(1) VALUE 'C'.

now, only when the value in WS-REGULAR-VAR is 'X' i want to use the values in WS-EXCEPTIONS.

what I am trying is shown below. can i use this or should i be using IN(CODE1,CODE2,CODE3) instead??

exec sql
select
--
--
where
my_field in
case
:WS-REGULAR-VAR = 'X'
then (:WS-EXCEPTIONS)
else (:WS-REGULAR-VAR)
end
and --
and --
with ur
end-exec
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed Sep 16, 2009 9:06 pm
Reply with quote

Hello,

Suggest you post some sample data and the result you want when that sample data is processed by this query.

When posting the sample data, use the Code tag for readability. Use Preview to see the post as it will appear to the forum (rather than what is seen in the Reply Editor).
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Fri Sep 18, 2009 3:36 pm
Reply with quote

Instead of WS-EXCEPTIONS give :CODE1,:CODE2,:CODE3 ...
Using CASE with an IN predicate should be with a fullselect.
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Fri Sep 18, 2009 3:50 pm
Reply with quote

best way would be : to do this before your SQL

Code:
01 WS-REGULAR-VAR PIC X(1).
01 WS-EXCEPTIONS.
   05 CODE1 PIC X(1) VALUE 'A'.
   05 CODE2 PIC X(1) VALUE 'B'.
   05 CODE3 PIC X(1) VALUE 'C'.
01 WS-HV.
   05 CODE1 PIC X(1).
   05 CODE2 PIC X(1).
   05 CODE3 PIC X(1).

if WS-REGULAR-VAR = 'X'
then move ws-exceptions to ws-HV
else move WS-REGULAR-VAR to CODE1 of ws-HV
     move WS-REGULAR-VAR to CODE2 of ws-HV
     move WS-REGULAR-VAR to CODE3 of ws-HV
end-if

exec sql
...
where my_field in (:WS-HV)
end-exec
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 COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts To search DB2 table based on Conditio... DB2 1
No new posts Zunit Test case editor error Testing & Performance 4
No new posts JOIN STATEMENT PERFORMANCE. DFSORT/ICETOOL 12
No new posts Relate COBOL statements to EGL statement All Other Mainframe Topics 0
Search our Forums:

Back to Top