Solved

Urgent:exact fetch returns more than requested number of rows

Posted on 2007-11-19
8
1,686 Views
Last Modified: 2013-12-19
hi
i am using this query in my function in oracle
---
BEGIN
   cursor c1 is
   select decode(operation,'MinFinal',test_id), decode(operation,'Final',test_id) into testid1,testid2
   from test.test_data_v7
   where operation in ('MinFinal','Final')
   and (pf = 1 or pf = 4) and serial = '' || in_serialno || '';

when i run the function i get this error
ORA-01422: exact fetch returns more than requested number of rows
do i need to use cursor then? this query is supposed to return 2 records if match found.
i
0
Comment
Question by:samir25
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 42

Expert Comment

by:dqmq
ID: 20310987
Remove this line:

into testid1,testid2
0
 
LVL 1

Author Comment

by:samir25
ID: 20310990
then how do i refer it..can u explian me more thanks
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 20310993
firstly, please paste your full code for a better understanding.

From the above, if you have more than one record returned in the select then either you should have bulk collect or you should use a cursor and iterate through the two records to fetch them into variable as shown below.

declare

   cursor c1 is
   select decode(operation,'MinFinal',test_id) test_id1, decode(operation,'Final',test_id) test_id2    from test.test_data_v7
   where operation in ('MinFinal','Final')
   and (pf = 1 or pf = 4) and serial = '' || in_serialno || '';

test_id1 varchar2(10); -- use number if test_id1 is number data type, also note i have used 10
test_id2 varchar2(10); -- use number if test_id1 is number data type

BEGIN

for a in c1
loop

    dbms_output.put_line('id1:'||test_id1);
    dbms_output.put_line('id2:'||test_id2);
    -- the above will just the variables fetched.. just do whatever processing you want
    -- there instead of printing them.

end loop;
end;
/
   
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 42

Expert Comment

by:dqmq
ID: 20311022
>then how do i refer it..can u explian me more thanks

Open the cursor, then fetch from the cursor into the variables.  Usually, you do it inside a loop because of the multiple rows.

0
 
LVL 1

Author Comment

by:samir25
ID: 20311089
here is what i did
CREATE OR REPLACE FUNCTION FORMAT(in_serialno varchar2)
RETURN LINES_TABLE PIPELINED IS
  cursor c1 is
   select decode(operation,'MinFinal',test_id) test_id1, decode(operation,'Final',test_id) test_id2    from test.test_data_v7
   where operation in ('MinFinal','Final')
   and (pf = 1 or pf = 4) and serial = '' || in_serialno || '';
some variables.....
...
..
BEGIN
 for r1 in c1
loop
   dbms_output.put_line('testid ' || r1.testID1);
   dbms_output.put_line('testid ' || r1.testID2);
end loop;
...
..
end;

no test id is outputted..why is this so?
0
 
LVL 1

Author Comment

by:samir25
ID: 20311098
btw i run it as
select * from table(FORMAT('2322503'))
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 20311137
you should do

SQL> SET serveroutput on   -- to see the output on screen.

I am not sure what you are trying to do. if you need more info. on pipelined functions, then refer to http://www.oracle-base.com/articles/9i/PipelinedTableFunctions9i.php

Thanks
0
 
LVL 28

Accepted Solution

by:
Naveen Kumar earned 250 total points
ID: 20311140
if you want non pipelined version of code.. it is something like the below :

CREATE OR REPLACE FUNCTION FORMAT(in_serialno varchar2)
RETURN LINES_TABLE IS
  cursor c1 is
   select decode(operation,'MinFinal',test_id) test_id1, decode(operation,'Final',test_id) test_id2    from test.test_data_v7
   where operation in ('MinFinal','Final')
   and (pf = 1 or pf = 4) and serial = '' || in_serialno || '';
v_tab LINES_TABLE := LINES_TABLE();
...
..
BEGIN
 for r1 in c1
loop
--   dbms_output.put_line('testid ' || r1.testID1);
--   dbms_output.put_line('testid ' || r1.testID2);
   v_tab.extend;
   v_tab(v_tab.last) := r1.test_id1;
   v_tab.extend;
   v_tab(v_tab.last) := r1.test_id2;
end loop;
return v_tab;
end;
/
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Via a live example, show how to take different types of Oracle backups using RMAN.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question