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 > Multiple rows from a SELECT statement?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-28-10, 09:42
Balr14 Balr14 is offline
Registered User
 
Join Date: Sep 2002
Location: Wisconsin
Posts: 37
Multiple rows from a SELECT statement?

I was under the impression a SELECT statement will fail if more than one row matches the selection criteria. Yet, I have someone who swears this works, although I have not seen any evidence to support this contention. It's a very simple SELECT, coming from a WebSphere SQL:

SELECT ACCT_NBR
FROM HOFF.T09_ACCOUNTS
WHERE CUST_ID = W_CUST_ID

I have no way of running this myself, except to code it in a COBOL program on the mainframe. I'm not sure that would prove anything.
Reply With Quote
  #2 (permalink)  
Old 10-28-10, 10:02
przytula_guy przytula_guy is offline
Registered User
 
Join Date: Apr 2006
Location: Belgium
Posts: 1,159
whenever there is more than 1 row - a cursor has to be declared and fetch has to be executed on cursor. otherwise a direct exec sql select into :hv ... can be used
__________________
Best Regards, Guy Przytula
Database Software Consultant
DB2 UDB LUW Certified V7-V8-V9-V9.7 DB Admin - Dprop..
Information Server Datastage Certified
http://www.infocura.be
Reply With Quote
  #3 (permalink)  
Old 10-28-10, 10:17
Balr14 Balr14 is offline
Registered User
 
Join Date: Sep 2002
Location: Wisconsin
Posts: 37
Please explain "direct exec sql select into :hv ". My DB2 programming skills are fairly basic. Does a dynamic SQL sent through DB2 connect return all rows of a result set at once?
Reply With Quote
  #4 (permalink)  
Old 10-28-10, 11:56
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
If you can run it via SPUFI, you can run it via DB2 Connect.

A query that returns a single row is fetched directly (no client side cursor is instantiated). If more than one row is returned, then the client instantiates a cursor and each row is fetched and returned. It appears to everything higher in the client's protocol stack like the entire result set is returned at once.

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #5 (permalink)  
Old 10-28-10, 12:26
Balr14 Balr14 is offline
Registered User
 
Join Date: Sep 2002
Location: Wisconsin
Posts: 37
So, no matter what, I need a cursor somewhere. That's what I figured. Thanks.
Reply With Quote
  #6 (permalink)  
Old 10-28-10, 12:38
przytula_guy przytula_guy is offline
Registered User
 
Join Date: Apr 2006
Location: Belgium
Posts: 1,159
have a look at application guide or cobol samples shipped how todo this
__________________
Best Regards, Guy Przytula
Database Software Consultant
DB2 UDB LUW Certified V7-V8-V9-V9.7 DB Admin - Dprop..
Information Server Datastage Certified
http://www.infocura.be
Reply With Quote
  #7 (permalink)  
Old 10-28-10, 15:05
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Lightbulb Singleton Select VS Cursor

Quote:
Originally Posted by Balr14 View Post
I was under the impression a SELECT statement will fail if more than one row matches the selection criteria. Yet, I have someone who swears this works, although I have not seen any evidence to support this contention. It's a very simple SELECT, coming from a WebSphere SQL:

SELECT ACCT_NBR
FROM HOFF.T09_ACCOUNTS
WHERE CUST_ID = W_CUST_ID

I have no way of running this myself, except to code it in a COBOL program on the mainframe. I'm not sure that would prove anything.
If you need only one row, you can get it using singleton select:

Code:
Exec SQL
 SELECT ACCT_NBR
 INTO :ws-acct-nbr:ws-acct-nbr-ind
 FROM HOFF.T09_ACCOUNTS
 WHERE CUST_ID = W_CUST_ID
 Fetch first row only 
End-Exec

if sqlcode < 0
  .........
end-if
Lenny
Reply With Quote
  #8 (permalink)  
Old 10-29-10, 06:01
tapas16880 tapas16880 is offline
Registered User
 
Join Date: Oct 2010
Posts: 5
It will give -811

For a singleton select, if multiple rows are fetched for the condition in the where predicate, it will throw -811 and that's why we go for CURSOR in DB2 and process each fetched row.

Hope this helps!!
Reply With Quote
  #9 (permalink)  
Old 10-29-10, 07:21
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Exclamation Read before

Quote:
Originally Posted by tapas16880 View Post
For a singleton select, if multiple rows are fetched for the condition in the where predicate, it will throw -811 and that's why we go for CURSOR in DB2 and process each fetched row.

Hope this helps!!
Quote:
Fetch first row only
- prevents sqlcode = -811

Kara
Reply With Quote
  #10 (permalink)  
Old 10-29-10, 10:11
Balr14 Balr14 is offline
Registered User
 
Join Date: Sep 2002
Location: Wisconsin
Posts: 37
Quote:
Originally Posted by DB2Plus View Post
- prevents sqlcode = -811

Kara
Now, that I recall!
Reply With Quote
  #11 (permalink)  
Old 11-01-10, 05:37
tapas16880 tapas16880 is offline
Registered User
 
Join Date: Oct 2010
Posts: 5
Other option

From Performace point of view instead of "fetch first row only", people do not prefer writing a select query. Instead it is always recommended to do an "existence check".
Select 1
into :ws-xxxx
from xxxxx
where ??? = :?????

This always works out faster!!!
Reply With Quote
  #12 (permalink)  
Old 11-01-10, 07:27
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Thumbs down

Quote:
Originally Posted by tapas16880 View Post
From Performace point of view instead of "fetch first row only", people do not prefer writing a select query. Instead it is always recommended to do an "existence check".
Select 1
into :ws-xxxx
from xxxxx
where ??? = :?????

This always works out faster!!!
I'm sorry, but you don't understand how DB2 works.

Even with yours "existence check" you can get -811.

Kara
Reply With Quote
  #13 (permalink)  
Old 11-02-10, 00:20
tapas16880 tapas16880 is offline
Registered User
 
Join Date: Oct 2010
Posts: 5
Talking Thanks - U don't have to teach me DB2

I have never said that "existence check" doesn't give -811. Did I? What solely I meant was instead of Select * or singleton select, it is always recommended to use "Existence check" when you just need to know whether the value is present in the table or not. Off course at the end of the SQL, you need to have ---

EVALUATE SQLCODE
WHEN +100
????????????????
WHEN -811
??????????
WHEN 0
?????????????
WHEN OTHER
??????
END-EVALUATE.
Reply With Quote
  #14 (permalink)  
Old 11-02-10, 05:58
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Quote:
Originally Posted by tapas16880 View Post
I have never said that "existence check" doesn't give -811. Did I? What solely I meant was instead of Select * or singleton select, it is always recommended to use "Existence check" when you just need to know whether the value is present in the table or not. Off course at the end of the SQL, you need to have ---

EVALUATE SQLCODE
WHEN +100
????????????????
WHEN -811
??????????
WHEN 0
?????????????
WHEN OTHER
??????
END-EVALUATE.
Could be better:

Code:
EVALUATE TRUE
SQLCODE
WHEN SQLCODE = +100
????????????????
WHEN 0
WHEN -811
?????????????
WHEN SQLCODE > 0
????????????????
WHEN OTHER
??????
END-EVALUATE.
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