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 > CASE function execution query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-02-12, 12:37
madsongtel madsongtel is offline
Registered User
 
Join Date: May 2011
Posts: 9
CASE function execution query

Hi All,

Below is my query, here i am not having any record which satisfies LOCATION ='123' in LOCATION_TB table then also my join query is executing and is giving -811 error due to more than 1 row returning when only one is expected, why it is executing like that.
when no row satisfies then it should not eecute Inner query right?
It should return ' ', please let me know how it is working ?

COALESCE(((Case
When SUBSTR(L.LOCATION,1,3) = '123' Then
(Select SUBSTR(LOC_CD,1,3) from POST Where POST_CD = L.POST_TX)
Else ' '
End ) ),( ' ' )) as ADDRESS_CODE from LOCATION_TB L
Reply With Quote
  #2 (permalink)  
Old 01-02-12, 13:33
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
why it is executing like that.
I'm not sure, but I guessed that result-subquery was evaluated before WHEN condition was evaluated.

To make the query workable, please try to use LEFT OUTER JOIN like in your previous thread
COALESCE Function conversion
Reply With Quote
  #3 (permalink)  
Old 01-02-12, 13:56
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Another simple way is to put WHEN condition into scalar-subquery, like...
Code:
SELECT
       COALESCE(
          (Select SUBSTR(LOC_CD,1,3)
            from  POST
            Where SUBSTR(L.LOCATION,1,3) = '123'
              AND POST_CD = L.POST_TX
          )
        , ' '
       ) as ADDRESS_CODE
 from  LOCATION_TB L
Note: I also added this solution into the thread
COALESCE Function conversion
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