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

HELP on SQL query statement.


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

Active User


Joined: 08 May 2008
Posts: 390
Location: China

PostPosted: Tue Nov 01, 2011 9:44 am
Reply with quote

I have many fields received from screen, and I have to preform query according to different combination of fields,each of these fields can be empty.

for example, I have four fields named A, B, C, D,
if A is empty, I have to execute SQL statement: select...from T WHERE B =.. AND C=...AND D=...

if B is empty, I have to execute SQL statement: select...from T WHERE A =.. AND C=...AND D=...

if C is empty, I have to execute SQL statement: select...from T WHERE A =.. AND B=...AND D=...

if A and C is empty, I have to execute SQL statement: select...from T WHERE B=...AND D=...
......

So ,as there are many combinations, I have to write different SQL statements for all these combinations , which is very very troublesome...


Can anybody suggest a simple way to do this? Is one SQL statement possible for this requirement?
Back to top
View user's profile Send private message
Pandora-Box

Global Moderator


Joined: 07 Sep 2006
Posts: 1592
Location: Andromeda Galaxy

PostPosted: Tue Nov 01, 2011 10:58 am
Reply with quote

I see you can use simple evaluate effectively or

make use of

1.Evaluate
2.Dynamic sql

Hope this helps
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Tue Nov 01, 2011 1:13 pm
Reply with quote

Quote:
if ... is empty,
icon_question.gif icon_question.gif icon_question.gif
after three years haven' t You learned the proper terminology yet ?

define empty in proper DB2 terms , please
Back to top
View user's profile Send private message
dejunzhu

Active User


Joined: 08 May 2008
Posts: 390
Location: China

PostPosted: Tue Nov 01, 2011 1:31 pm
Reply with quote

hi, enrico,

when I say a field is empty, I mean that field has a value of SPACE if it is defined as CHAR type.
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Tue Nov 01, 2011 1:38 pm
Reply with quote

if You search the forums the issue has already been discussed a few times
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Tue Nov 01, 2011 1:43 pm
Reply with quote

You've already been given what looks to me a good answer by premkrishan.

If it is possible to do in one SQL statement, it might be less tiring to type, but it will take much, much, longer for anyone on maintenance/support to understand it.

If you don't want to code it all out (my choice, with an editor available I'm not scared of the small amount of typing necessary) then generate the dynamic SQL. The SQL generated will be as simple as that coded out, but the code to generate it will be less clear. 'S why I favour the first.
Back to top
View user's profile Send private message
dejunzhu

Active User


Joined: 08 May 2008
Posts: 390
Location: China

PostPosted: Tue Nov 01, 2011 4:39 pm
Reply with quote

I have been told one solution as below:

if A = ' ' THEN
MOVE LOW-VALUE TO A1
MOVE HIGH-VALUE TO A2

ELSE
MOVE A TO A1
MOVE A TO A2

END-IF

IF B = ' ' THEN
...
...

SELECT ...FROM T
WHERE A BETWEEN A1 AND A2
AND B BETWEEN B1 AND B2
AND C BETWEEN C1 AND C2




Can you please advise on above solution?
what's the pros and cons? (Maybe the efficiency will suffer...)
Back to top
View user's profile Send private message
Pandora-Box

Global Moderator


Joined: 07 Sep 2006
Posts: 1592
Location: Andromeda Galaxy

PostPosted: Tue Nov 01, 2011 4:49 pm
Reply with quote

First place you got or found a much simpler solution and
I dont have the table T with me so neither the columns icon_smile.gif

Why dont you try yourself on EXPLAIN?
If you are not able to comprehend the EXPLAIN report check the Manual

If you dont understand still then post here


As I dont see no relation with your Topic and your last post
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 RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Query on edit primary command CLIST & REXX 5
No new posts Query on edit primary command CLIST & REXX 1
No new posts JOIN STATEMENT PERFORMANCE. DFSORT/ICETOOL 12
Search our Forums:

Back to Top