If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > writing query in then part of case statment

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-17-11, 04:59
c_jai c_jai is offline
Registered User
 
Join Date: May 2011
Posts: 1
Thumbs up writing query in then part of case statment

hi

is it possible to write a select query in 'then' part of case statment as below?
I wanted to execute the appropriate select statment based on the condition.

case
when <condition> then <select query>
else <Another select query>
end
Reply With Quote
  #2 (permalink)  
Old 05-17-11, 08:34
jsharon1248 jsharon1248 is offline
Registered User
 
Join Date: Apr 2007
Location: Chicago
Posts: 57
The embedded query is called a Scalar Full Select, and yes, it is allowed as a CASE expression.

Quote:
A scalar-fullselect as supported in an expression is a fullselect,
enclosed in parentheses, that returns a single row consisting of a single
column value. If the fullselect does not return a row, the result of the
expression is the null value. If more than one row is to be returned for a
scalar fullselect, an error occurs.
Reply With Quote
  #3 (permalink)  
Old 05-17-11, 08:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
jsharon, i'm pretty sure that c_jai does not want "a single row consisting of a single column value"

c_jai, could you please show both of your select queries
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 05-17-11, 09:10
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
You can use CASE statement(not CASE expression) in a compound SQL.
But, if used SELECT statement(s) in a compound SQL, problem is how to get the result.

If number of items and data types of them in the reslts of the two select statements were identical,
you may want to use something like...
<select query>
WHERE <condition>
UNION ALL
<Another select query>
WHERE CASE <condition> THEN 0 ELSE 1 END = 1

If <condition> was always true or false(that means no unkown result)
<select query>
WHERE <condition>
UNION ALL
<Another select query>
WHERE NOT <condition>

If <select query> or <Another select query> include WHERE clause,
put all the where conditions in parentheses then add "AND <condition>" or "AND NOT <condition>".

Last edited by tonkuma; 05-17-11 at 09:16. Reason: Add last sentence.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On