【发布时间】:2018-12-14 12:05:41
【问题描述】:
有没有办法解决“ORA-01422 fetch return more than requested number of >rows”而不使用 for 循环来返回表函数的值?使用 for 循环获取数据以将该数据用于 oracle 应用程序使用 APEX 会变慢。
【问题讨论】:
标签: oracle plsql oracle11g plsqldeveloper oracle-apex-5
有没有办法解决“ORA-01422 fetch return more than requested number of >rows”而不使用 for 循环来返回表函数的值?使用 for 循环获取数据以将该数据用于 oracle 应用程序使用 APEX 会变慢。
【问题讨论】:
标签: oracle plsql oracle11g plsqldeveloper oracle-apex-5
您可以针对 ORA-01422 使用 DISTINCT 或 GROUP BY 并列出所有应为的行不使用 LOOP 而返回,但只有一个 INTO,如下例所示:
SQL> create table tab( col1 int, col2 varchar2(50) );
Table created
SQL> insert all
2 into tab values(1,'abc')
3 into tab values(1,'abc')
4 into tab values(2,'def')
5 select * from dual;
3 rows inserted
SQL> set serveroutput on;
SQL>
SQL> declare
2 v_col1 tab.col1%type;
3 v_col2 tab.col2%type;
4 begin
5 select t.col1, t.col2
6 into v_col1, v_col2
7 from tab t
8 where t.col1 = 1;
9
10 dbms_output.put_line(v_col1||' '||v_col2);
11 end;
12 /
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at line 6
SQL>
SQL> declare
2 v_col1 tab.col1%type;
3 v_col2 tab.col2%type;
4 begin
5 select t.col1, t.col2
6 into v_col1, v_col2
7 from tab t
8 where t.col1 = 1
9 group by t.col1, t.col2;
10
11 dbms_output.put_line(v_col1||' '||v_col2);
12 end;
13 /
1 abc
PL/SQL procedure successfully completed
SQL>
SQL> declare
2 v_col1 tab.col1%type;
3 v_col2 tab.col2%type;
4 begin
5 select distinct t.col1, t.col2
6 into v_col1, v_col2
7 from tab t
8 where t.col1 = 1;
9
10 dbms_output.put_line(v_col1||' '||v_col2);
11 end;
12 /
1 abc
PL/SQL procedure successfully completed
【讨论】:
insert into tab values (1, 'def');。我并不是说您写的内容错误,而是我们没有足够的信息来提供正确答案。我认为 OP 应该提供更多信息。