Oracle PL/SQL
Home > Ask the Oracle Experts > PL/SQL Questions & Answers > ORA-01422 error when procedure returns more than one row
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

ORA-01422 error when procedure returns more than one row

Greg Williams EXPERT RESPONSE FROM: Greg Williams

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 13 July 2007

Mr. Williams, I have the following Oracle procedure that returns one or more rows depending upon the PO ID input into the SQL statement. When there is greater than one row returned I get the ORA-01422 error. How do I recode this procedure to allow for one to many returned rows of data? The PO ID (input) will be passed into the proc by another program. I have hardcoded the PO ID ('0700185') for testing pursposes. The proc is supposed to return all vouchers associated with this PO. There may be one voucher, there may be five vouchers.

Please be advised I am not a DBA, I am a pure novice at Oracle procedures, but willing to learn. Thank you for any assistance you may provide.

CREATE OR REPLACE PROCEDURE TESTPNET(PO OUT VARCHAR, BATCH OUT VARCHAR2, VOUCHER OUT VARCHAR2, INVOICE OUT VARCHAR2) AS BEGIN SELECT PO_ID,GRP_AP_ID, VOUCHER_ID, INVOICE_ID INTO PO, BATCH, VOUCHER, INVOICE FROM PS_VOUCHER WHERE PO_ID = '0700185'; DBMS_OUTPUT.PUT_LINE(PO); DBMS_OUTPUT.PUT_LINE(BATCH); DBMS_OUTPUT.PUT_LINE(VOUCHER); DBMS_OUTPUT.PUT_LINE(INVOICE); END TESTPNET;


>
EXPERT RESPONSE
The procedure you have created is using a SQL that should only be used if you are expecting to return only a single row. If you are expecting to return more then one row, you should use a cursor. I have added a cursor called c1 (see the second procedure). The second procedure uses a loop to read through the selected records.
CREATE OR REPLACE PROCEDURE testpnet (
   po        OUT   VARCHAR,
   batch     OUT   VARCHAR2,
   voucher   OUT   VARCHAR2,
   invoice   OUT   VARCHAR2
)
AS

BEGIN
   SELECT po_id, grp_ap_id, voucher_id, invoice_id
     INTO po, batch, voucher, invoice
     FROM ps_voucher
    WHERE po_id = '0700185';
   close;
       
   DBMS_OUTPUT.put_line (po);
   DBMS_OUTPUT.put_line (batch);
   DBMS_OUTPUT.put_line (voucher);
   DBMS_OUTPUT.put_line (invoice);
END testpnet;

/************* Cursor **************/

CREATE OR REPLACE PROCEDURE testpnet (
   po        OUT   VARCHAR,
   batch     OUT   VARCHAR2,
   voucher   OUT   VARCHAR2,
   invoice   OUT   VARCHAR2
)
AS

cursor c1 is
   select po_id, grp_ap_id, voucher_id, invoice_id
   from ps_voucher
   where po_id = '0700185'; 

BEGIN

   open c1;
   loop
       fetch c1 into po, batch, voucher, invoice;
       exit when c1%NOTFOUND;
       <do something>
   end loop;
   close;
       
   DBMS_OUTPUT.put_line (po);
   DBMS_OUTPUT.put_line (batch);
   DBMS_OUTPUT.put_line (voucher);
   DBMS_OUTPUT.put_line (invoice);
END testpnet;


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
PL/SQL
Converting Long Raw to Blob
Parsing in Oracle
Definition of force view
ORA-04082: NEW or OLD references not allowed in table level triggers
Execute SQL statement from table in other schema
Script to revoke access from user
PL/SQL procedure to load CSV file into database table
Finding size of files in BLOB datatype
Expression is of wrong type
Sorting a clob column

Oracle PL/SQL
Oracle's free SQL Developer adds database migration tool
Confused about Oracle certification exams
Calling procedure inside another procedure in anonymous block
How to import comma-delimited text file to Oracle table
Oracle updates Microsoft developer tools
PLS-00103 errors
PL/SQL do's and don't's: Five questions with Steven Feuerstein
Definition of force view
ORA-04082: NEW or OLD references not allowed in table level triggers
Oracle updates free Web development tool

Oracle stored procedures
Stored procedure to autotransfer data between Oracle servers
Insufficient privileges error when creating stored procedure
Calling procedure inside another procedure in anonymous block
Can I make a second connection to Oracle without losing the first?
How to create an index using a procedure in Oracle
Oracle updates Microsoft developer tools
Procedure compiles but does not execute
Tool in Oracle 10g to keep track of execution of queries
ORA-04082: NEW or OLD references not allowed in table level triggers
Execute SQL statement from table in other schema

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
PL/SQL  (SearchOracle.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsWebcastsWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts