Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

HELP on SQL query statement.

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: HELP on SQL query statement.
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

Moderator


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

PostPosted: Tue Nov 01, 2011 10:58 am    Post subject:
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

Global Moderator


Joined: 14 Mar 2007
Posts: 10256
Location: italy

PostPosted: Tue Nov 01, 2011 1:13 pm    Post subject: Reply to: HELP on SQL query statement.
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    Post subject:
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

Global Moderator


Joined: 14 Mar 2007
Posts: 10256
Location: italy

PostPosted: Tue Nov 01, 2011 1:38 pm    Post subject: Reply to: HELP on SQL query statement.
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

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7315

PostPosted: Tue Nov 01, 2011 1:43 pm    Post subject: Reply to: HELP on SQL query statement.
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    Post subject:
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

Moderator


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

PostPosted: Tue Nov 01, 2011 4:49 pm    Post subject:
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    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts JCL to Set Return code based on DB2 S... vinu78 DB2 17 Mon Mar 13, 2017 9:47 pm
No new posts SQL query not working in Cobol program. CuriousMainframer COBOL Programming 14 Wed Feb 22, 2017 5:56 pm
No new posts Omegamon for DB2 query ashek15 DB2 0 Wed Feb 15, 2017 11:25 am
No new posts Query on DB2 WLM stored procedures ashek15 DB2 8 Sun Feb 12, 2017 11:13 pm
No new posts ROUNDED Problem with COMPUTE statement shalem COBOL Programming 11 Thu Feb 09, 2017 8:16 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us