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

-811 in db2 CASE Statement


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

New User


Joined: 11 Oct 2004
Posts: 69
Location: chennai

PostPosted: Tue Sep 16, 2014 5:23 pm
Reply with quote

Hi ,

I am trying to execute the below query
Code:
SELECT * FROM USI.TSSIMB
WHERE SIMB_CD_BP IN
(CASE
WHEN  (SELECT COUNT(*) FROM USI.TSSIMD
WHERE SIMD_ID_SIM = 23
AND SIMD_TYPE ='BP') > 0
THEN
(SELECT SIMD_VALUE1 FROM USI.TSSIMD
WHERE  SIMD_TYPE ='BP'
AND    SIMD_ID_SIM = 23)
ELSE
(SELECT DISTINCT SIMB_CD_BP FROM USI.TSSIMB)
END);


and i am getting sqlcode -811 with below description

Code:
DSNT408I SQLCODE = -811, ERROR:  THE RESULT OF AN EMBEDDED SELECT STATEMENT OR
A SUBSELECT IN THE SET CLAUSE OF AN UPDATE STATEMENT IS A TABLE OF
MORE THAN ONE ROW, OR THE RESULT OF A SUBQUERY OF A BASIC PREDICATE
IS MORE THAN ONE VALUE


Please Guide me , how to solve this issue
Back to top
View user's profile Send private message
Nic Clouston

Global Moderator


Joined: 10 May 2007
Posts: 2455
Location: Hampshire, UK

PostPosted: Tue Sep 16, 2014 6:13 pm
Reply with quote

Use DISTINCT as per your last line of the query. If that is not an option and you need one row then add more WHERE conditions.
Back to top
View user's profile Send private message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3049
Location: NYC,USA

PostPosted: Wed Sep 17, 2014 12:12 am
Reply with quote

use
Code:
 FETCH FIRST ROW ONLY
but it looks like you need to place this under cursor as you have a IN operator which meant to get more than 1 row.
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Mon Oct 06, 2014 2:51 pm
Reply with quote

A case can only return 1 value.
I think you want
Code:
SELECT * FROM USI.TSSIMB
where  SIMB_CD_BP IN
   (SELECT SIMD_VALUE1 FROM USI.TSSIMD
     WHERE  SIMD_TYPE ='BP'
       AND SIMD_ID_SIM = 23)
or
 not exists (SELECT SIMD_VALUE1 FROM USI.TSSIMD
           WHERE  SIMD_TYPE ='BP'
              AND SIMD_ID_SIM = 23)
Back to top
View user's profile Send private message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3049
Location: NYC,USA

PostPosted: Wed Oct 08, 2014 3:46 am
Reply with quote

GuyC , I think you might wants to consider
Code:
SELECT DISTINCT SIMB_CD_BP FROM USI.TSSIMB
condition as well. But I may be missing something.

He already might have got the solution but can also try below IF HE HAS A COBOL PROGRAM.

Code:
SELECT col1,col2.....,'Y' FROM USI.TSSIMB
WHERE SIMB_CD_BP IN
(SELECT SIMD_VALUE1 FROM USI.TSSIMD
WHERE  SIMD_TYPE ='BP'
AND    SIMD_ID_SIM = 23)
UNION
SELECT col1,col2.....,'N' FROM USI.TSSIMB
WHERE SIMB_CD_BP IN
(SELECT DISTINCT SIMB_CD_BP FROM USI.TSSIMB)

FETCH FIRST ROW ONLY;


And then check for Y and N in COBOL and process accordingly. I mean if Y is found then ignore 'N' else process 'N' record.
Back to top
View user's profile Send private message
chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 275
Location: Mumbai

PostPosted: Wed Oct 08, 2014 5:23 pm
Reply with quote

Hi,

I guess OP want to give 1st preference to below criteria
Code:
SELECT SIMD_VALUE1 FROM USI.TSSIMD
WHERE  SIMD_TYPE ='BP'
AND    SIMD_ID_SIM = 23


if does not find anything with above criteria then only go for below criteria
Code:
SELECT DISTINCT SIMB_CD_BP FROM USI.TSSIMB


Ranjan, Can you please confirm this is what your requirement is?

May be I am missing something, but GuyC's query will return rows with both conditions.

Rohit's query seems to work fine if row with 'Y' come on top always as a result of UNION query.

But my question to OP is why DISTINCT is required there as anyways with IN clause you will return multiple rows for given SIMB_CD_BP if there are any in table.

Correct me if I am missing anything

Regards,
Chandan
Back to top
View user's profile Send private message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3049
Location: NYC,USA

PostPosted: Thu Oct 09, 2014 5:28 am
Reply with quote

Quote:

But my question to OP is why DISTINCT is required there as anyways with IN clause you will return multiple rows for given SIMB_CD_BP if there are any in table.


Right, Thats is why in my first reply I suggested this, Let us wait for TS to reply. However, DISTINCT is to get unique and multiple SIMB_CD_BP ( so that should be fine for him per his requirement)

Quote:
Rohit's query seems to work fine if row with 'Y' come on top always as a result of UNION query.


by adding
Code:
order by 2 desc
Back to top
View user's profile Send private message
chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 275
Location: Mumbai

PostPosted: Thu Oct 09, 2014 4:13 pm
Reply with quote

Quote:

However, DISTINCT is to get unique and multiple SIMB_CD_BP ( so that should be fine for him per his requirement)


my concern with DISTINCT was, irrespective of having distinct or not query result not going to change, but it will impact on performance with having distnict

Yes lets see if TS replies
Back to top
View user's profile Send private message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3049
Location: NYC,USA

PostPosted: Fri Oct 10, 2014 4:38 am
Reply with quote

Quote:
my concern with DISTINCT was, irrespective of having distinct or not query result not going to change, but it will impact on performance with having distnict

Why will it not change the result? If I remove the DISTINCT then TS would be getting unwanted duplicate rows for same SIMB_CD_BP.

But the whole point is unless TS use the cursor or say 'FETCH FIRST ROW ONLY' to the query ,DQLCODE -811 would certainly be persist.
Back to top
View user's profile Send private message
chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 275
Location: Mumbai

PostPosted: Sat Oct 11, 2014 11:43 pm
Reply with quote

Hi Rohit,

Let me try to explain my view with distinct for below qurey
Code:
WHERE SIMB_CD_BP IN
(SELECT DISTINCT SIMB_CD_BP FROM USI.TSSIMB)


Let say table has following data for SIMB_CD_BP

Code:
SIMB_CD_BP
1
1
2
2
3
3
3


Then for above data below both WHERE clause will return the same set of rows

Code:
WHERE SIMB_CD_BP IN
(1,2,3)

WHERE SIMB_CD_BP IN
(1,2,3)


Kindly correct me if I missing anything here as I don't have system to check right now.

Regards,
Chandan
Back to top
View user's profile Send private message
chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 275
Location: Mumbai

PostPosted: Sun Oct 12, 2014 12:11 am
Reply with quote

Some more information on duplicates in IN clause

ibmmainframes.com/about42673.html

Regards,
Chandan
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Mon Oct 20, 2014 5:38 pm
Reply with quote

did anyone notice this :
Code:
SELECT * FROM mytable WHERE mycolumn IN
(SELECT mycolumn FROM mytable)

I'm pretty sure this will always be true
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 COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts Zunit Test case editor error Testing & Performance 4
No new posts JOIN STATEMENT PERFORMANCE. DFSORT/ICETOOL 12
No new posts Relate COBOL statements to EGL statement All Other Mainframe Topics 0
No new posts process statement for SUPREC, CMPCOLM... TSO/ISPF 4
Search our Forums:

Back to Top