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 > strange result while selecting from a table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-01-10, 15:48
surjyakp surjyakp is offline
Registered User
 
Join Date: Jan 2010
Posts: 26
strange result while selecting from a table

Hi,
I executed the below select stmt alone and gives the corect result
select pq5.incdnt_all.usrid from pq5.incdnt_all where pq5.incdnt_all.incdnt_id = 'Incdnt_ID'

But when I execute it through store procedure like below , it returns multiple row.
DECLARE USERID INTEGER DEFAULT 0;
SET USERID=(select pq5.incdnt_all.usrid from pq5.incdnt_all where pq5.incdnt_all.incdnt_id = Incdnt_ID);

How is it possible? Please provide some solution to resolve this issue.

Thanks
Surjya
Reply With Quote
  #2 (permalink)  
Old 02-01-10, 16:18
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by surjyakp View Post
Hi,


But when I execute it through store procedure like below , it returns multiple row.
DECLARE USERID INTEGER DEFAULT 0;
SET USERID=(select pq5.incdnt_all.usrid from pq5.incdnt_all where pq5.incdnt_all.incdnt_id = Incdnt_ID);
Your subselect is equivalent to
Code:
select pq5.incdnt_all.usrid from pq5.incdnt_all where 1 = 1
which obviously returns all rows in the table. You should use some kind of naming convention that distinguishes table column names from variable and parameter names.
Reply With Quote
  #3 (permalink)  
Old 02-01-10, 16:19
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
Are you sure it returns multiple rows, or are you just getting an error message that it might return multiple rows and therefore cannot be used in the context of statement you are attempting to use it in?
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #4 (permalink)  
Old 02-01-10, 16:50
surjyakp surjyakp is offline
Registered User
 
Join Date: Jan 2010
Posts: 26
Yes instead of retriving one column I sued
SET USERID=(select count(*) from pq5.incdnt_all where pq5.incdnt_all.incdnt_id = IncdntID);
It gives total number of records in that table instead of for the particular incdnt_id. incdnt_id is primary key in that table.

Thanks
Surjya
Reply With Quote
  #5 (permalink)  
Old 02-01-10, 19:44
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
I think your question is same as this thread for which I already answered same as Nick Ivanov's answer.

error while selecting

Last edited by tonkuma; 02-01-10 at 19:46. Reason: Refer to Nick Ivanov.
Reply With Quote
  #6 (permalink)  
Old 02-02-10, 05:01
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
If you look at the 2 queries closely, you'll see that both are NOT identical. In one case, you compare with a string with the content "'Incdnt_ID'" and in the other case you use the content of the variable named Incdnt_ID because you don't have any quotes around the "Incdnt_ID".
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
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