[英]ORA-01422: exact fetch returns more than requested number of rows. Can I return all the rows in a table with this query?
select idstatus, paymonths
into status, monthly_payment_plan
from dd_pledge
where iddonor = donor_id;
I'm trying to retrieve all the rows returned from this query.我正在尝试检索从此查询返回的所有行。 It's working fine when I have only one row but if there is more than one row I get this error:
当我只有一行时它工作正常,但如果有不止一行,我会收到此错误:
ORA-01422: exact fetch returns more than the requested number of rows.
ORA-01422: 精确提取返回的行数多于请求的行数。
Some of the answers I checked on the internet said use aggregate function and then group by clause, but I want to display all the returned values in a table.我在互联网上查看的一些答案说使用聚合 function 然后按子句分组,但我想在表格中显示所有返回值。
Thank you.谢谢你。
With SELECT... INTO
you can return a collection as single row.使用
SELECT... INTO
您可以将集合作为单行返回。
Try the following reproducible example:尝试以下可重现的示例:
create or replace type onerow as object (name varchar2 (16), value varchar2 (96));
/
create or replace type rowtab as table of onerow;
/
create table tab (name, value) as
select 'name'||rownum, 'value'||rownum
from dual connect by level<=3
/
var rc refcursor
declare
tab rowtab;
begin
select cast (multiset (
select name, value
from tab) as rowtab) into tab
from dual;
open :rc for select * from table (tab);
end;
/
print rc
Outcome:结果:
NAME VALUE
---------------- ----------------
name1 value1
name2 value2
name3 value3
You'll need something different than scalar variables.您将需要与标量变量不同的东西。 For example:
例如:
SQL> create or replace type t_row as object (ename varchar2(10), job varchar2(10));
2 /
Type created.
SQL> create or replace type t_tab as table of t_row;
2 /
Type created.
SQL> set serveroutput on
SQL> declare
2 l_tab t_tab;
3 begin
4 select t_row(ename, job)
5 bulk collect into l_tab
6 from emp
7 where deptno = 20;
8
9 for i in l_tab.first .. l_tab.last loop
10 dbms_output.put_line(l_tab(i).ename ||' '|| l_tab(i).job);
11 end loop;
12 end;
13 /
JONES MANAGER
SCOTT ANALYST
FORD ANALYST
SMITH CLERK
ADAMS CLERK
PL/SQL procedure successfully completed.
SQL>
If you are using SELECT... INTO...
then you can only return a single row.如果您使用的是
SELECT... INTO...
那么您只能返回一行。
If you want multiple rows then you would need to SELECT... BULK COLLECT INTO...
and put the values into collection variables.如果您想要多行,那么您需要
SELECT... BULK COLLECT INTO...
并将值放入集合变量中。 Or else, you could change to using a cursor .否则,您可以更改为使用 cursor 。
For example:例如:
DECLARE
TYPE t_statuses IS TABLE OF DD_PLEDGE.STATUS%TYPE;
TYPE t_monthly_payment_plans IS TABLE OF DD_PLEDGE.MONTHLY_PAYMENT_PLAN%TYPE;
p_statuses t_statuses;
p_monthly_payment_plans t_monthly_payment_plans;
p_donor_id DD_PLEDGE.IDDONOR%TYPE := 2;
BEGIN
SELECT status, monthly_payment_plan
BULK COLLECT INTO p_statuses, p_monthly_payment_plans
FROM dd_pledge
WHERE iddonor = p_donor_id;
FOR i IN 1 .. p_statuses.COUNT LOOP
DBMS_OUTPUT.PUT_LINE( p_statuses(i) || ', ' || p_monthly_payment_plans(i) );
END LOOP;
END;
/
or或者
DECLARE
TYPE t_pledges IS TABLE OF DD_PLEDGE%ROWTYPE;
p_pledges t_pledges;
p_donor_id DD_PLEDGE.IDDONOR%TYPE := 2;
BEGIN
SELECT *
BULK COLLECT INTO p_pledges
FROM dd_pledge
WHERE iddonor = p_donor_id;
FOR i IN 1 .. p_pledges.COUNT LOOP
DBMS_OUTPUT.PUT_LINE( p_pledges(i).status || ', ' || p_pledges(i).monthly_payment_plan );
END LOOP;
END;
/
With the sample data:使用样本数据:
CREATE TABLE DD_PLEDGE (
IDDONOR NUMBER(4),
PLEDGEAMT NUMBER(8,2),
STATUS CHAR(1),
MONTHLY_PAYMENT_PLAN VARCHAR2(20)
);
INSERT INTO dd_pledge ( iddonor, pledgeamt, status, monthly_payment_plan )
SELECT 1, 251.00, 'A', 'YES' FROM DUAL UNION ALL
SELECT 2, 250.01, 'B', 'NO' FROM DUAL;
Both output: output:
B, NO
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.