在 sql 开发者中运行简单选择时出错

问题描述 投票:0回答:1

我使用sql developer,只想测试一个简单的select查询,只有一个变量(P_SRV_CATEGORY_ID)。但是无法运行这个查询。

declare P_SRV_CATEGORY_ID number := 1;
BEGIN
    SELECT ID FROM INV_SRV1 WHERE SRV_CATEGORY_ID IN (
    SELECT id
    FROM   inv_srv_category1
    START WITH parent_category_id = P_SRV_CATEGORY_ID
    CONNECT BY PRIOR id = parent_category_id) OR SRV_CATEGORY_ID = P_SRV_CATEGORY_ID;
END;

错误(我真的不明白)是:

Error starting at line : 2 in command -
declare P_SRV_CATEGORY_ID number := 1;
BEGIN
    SELECT ID FROM INV_SRV1 WHERE SRV_CATEGORY_ID IN (
    SELECT id
    FROM   inv_srv_category1
    START WITH parent_category_id = P_SRV_CATEGORY_ID
    CONNECT BY PRIOR id = parent_category_id) OR SRV_CATEGORY_ID = P_SRV_CATEGORY_ID;
END;
Error report -
ORA-06550: line 3, column 5:
PLS-00428: an INTO clause is expected in this SELECT statement
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:
oracle plsql
1个回答
1
投票

如你所言。INTO 缺少了子句

declare 
  p_srv_category_id number := 1;
  l_id              inv_srv1.id%type;       --> this
begin
  select id 
  into l_id                                 --> this
  from inv_srv1 
  where srv_category_id in (select id
                            from inv_srv_category1
                            start with parent_category_id = p_srv_category_id
                            connect by prior id = parent_category_id
                           ) 
     or srv_category_id = p_srv_category_id;
end;

另外,我建议你使用表的别名。如果没有表别名,就很难猜到哪一列属于哪个表;Oracle有时也会感到困惑,从而得到你不希望看到的结果。


截至 too_many_rows 你有。我没有你的表格,所以我会在斯科特的EMP表上给你看: 第十部门有几个员工。

SQL> select empno, ename from emp where deptno = 10;

     EMPNO ENAME
---------- ----------
      7782 CLARK
      7839 KING
      7934 MILLER

这就是你要做的事

SQL> declare
  2    l_empno emp.empno%type;
  3  begin
  4    select empno
  5      into l_empno
  6      from emp
  7      where deptno = 10;
  8  end;
  9  /
declare
*
ERROR at line 1:
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at line 4

不过,这还算好用

SQL> declare
  2    l_arr sys.odcinumberlist;
  3  begin
  4    select empno
  5      bulk collect
  6      into l_arr
  7      from emp
  8      where deptno = 10;
  9  end;
 10  /

PL/SQL procedure successfully completed.

SQL>

如果你想真正的... 看到 的东西,这里有一个选项;注意附加的 FORALL:

SQL> set serveroutput on;
SQL> declare
  2    l_arr sys.odcinumberlist;
  3  begin
  4    select empno
  5      bulk collect
  6      into l_arr
  7      from emp
  8      where deptno = 10;
  9
 10    forall i in 1 .. l_arr.count
 11      execute immediate 'call dbms_output.put_line(:1)' using l_arr(i);
 12  end;
 13  /
7782
7839
7934

PL/SQL procedure successfully completed.

SQL>
© www.soinside.com 2019 - 2024. All rights reserved.