View previous topic :: View next topic
|
Author |
Message |
dejunzhu
Active User
Joined: 08 May 2008 Posts: 390 Location: China
|
|
|
|
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 |
|
|
Pandora-Box
Global Moderator
Joined: 07 Sep 2006 Posts: 1592 Location: Andromeda Galaxy
|
|
|
|
I see you can use simple evaluate effectively or
make use of
1.Evaluate
2.Dynamic sql
Hope this helps |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
after three years haven' t You learned the proper terminology yet ?
define empty in proper DB2 terms , please |
|
Back to top |
|
|
dejunzhu
Active User
Joined: 08 May 2008 Posts: 390 Location: China
|
|
|
|
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 |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
if You search the forums the issue has already been discussed a few times |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
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 |
|
|
dejunzhu
Active User
Joined: 08 May 2008 Posts: 390 Location: China
|
|
|
|
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 |
|
|
Pandora-Box
Global Moderator
Joined: 07 Sep 2006 Posts: 1592 Location: Andromeda Galaxy
|
|
|
|
First place you got or found a much simpler solution and
I dont have the table T with me so neither the columns
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 |
|
|
|