oracle PL/SQL过程SELECT输入-错误PLS-00306

6mw9ycah  于 2022-12-03  发布在  Oracle
关注(0)|答案(3)|浏览(211)

您好,我是PL/SQL的初学者,希望您能提供一些帮助。
因此,我在这里创建了这个过程,我的目标是在执行该过程时,输入一个5位整数(邮政编码),然后从表中选择这些值并显示,就像我执行了如下查询一样

SELECT * FROM customers WHERE customer_zipcode = "input zipcode".

create or replace PROCEDURE LIST_CUSTOMER_ZIPCODE(
p_zipcode IN customers.customer_zipcode%TYPE,
p_disp OUT SYS_REFCURSOR)
-- User input Variable, Display Variable
IS
BEGIN
    OPEN p_disp for SELECT customer_first_name, customer_zipcode FROM customers 
    WHERE customer_zipcode=p_zipcode;
EXCEPTION
    -- Input Sanitization
    WHEN no_data_found THEN
    dbms_output.put_line('-1');
END;

EXEC LIST_CUSTOMER_ZIPCODE(07080);

当我执行这个命令时,我总是得到这个错误。
https://i.stack.imgur.com/nCI8T.png

flmtquvp

flmtquvp1#

如果您使用的是SQL*Plus或SQL Developer,则可以声明一个绑定变量,然后调用传递该变量的过程并打印该变量:

SELECT * FROM customers WHERE customer_zipcode = "input zipcode".

create or replace PROCEDURE LIST_CUSTOMER_ZIPCODE(
  p_zipcode IN customers.customer_zipcode%TYPE,
  p_disp OUT SYS_REFCURSOR
)
IS
BEGIN
  OPEN p_disp FOR
    SELECT customer_first_name, customer_zipcode
    FROM   customers 
    WHERE  customer_zipcode = p_zipcode;
EXCEPTION
  -- Input Sanitization
  WHEN no_data_found THEN
    dbms_output.put_line('-1');
END;
/

VARIABLE cur SYS_REFCURSOR;

EXEC LIST_CUSTOMER_ZIPCODE('07080', :cur);

PRINT cur;

但是,您的异常处理块永远不会被调用,因为游标可以返回零行而不会引发该异常,因此该过程可以简化为:

create or replace PROCEDURE LIST_CUSTOMER_ZIPCODE(
  p_zipcode IN customers.customer_zipcode%TYPE,
  p_disp OUT SYS_REFCURSOR
)
IS
BEGIN
  OPEN p_disp FOR
    SELECT customer_first_name, customer_zipcode
    FROM   customers 
    WHERE  customer_zipcode = p_zipcode;
END;
/
aiazj4mn

aiazj4mn2#

您不能只是 * 执行 * 这样的过程,因为它需要2个参数;一个是IN,另一个是OUT(包含结果集引用游标)。
我没有您的表,因此我将使用Scott的示例模式进行演示,方法是传递部门编号并返回在该部门工作的雇员列表。
程序:

SQL> set serveroutput on
SQL> create or replace procedure p_list
  2    (p_deptno    in dept.deptno%type,
  3     p_disp     out sys_refcursor
  4    )
  5  is
  6  begin
  7    open p_disp for select ename, job from emp
  8                    where deptno = p_deptno;
  9  end;
 10  /

Procedure created.

您可以这样使用它:

SQL> declare
  2    l_list   sys_refcursor;
  3    l_ename  emp.ename%type;
  4    l_job    emp.job%type;
  5  begin
  6    p_list(10, l_list);       --> calling the procedure; use 2 parameters
  7
  8    loop
  9      fetch l_list into l_ename, l_job;
 10      exit when l_list%notfound;
 11      dbms_output.put_line(l_ename ||' - '|| l_job);
 12    end loop;
 13  end;
 14  /
CLARK - MANAGER
KING - PRESIDENT
MILLER - CLERK

PL/SQL procedure successfully completed.

SQL>
fhg3lkii

fhg3lkii3#

请尝试执行列表_客户_邮政编码(:p_邮政编码);
如果你把':'放在任何字符串之前,它将成为替换字符串,你可以键入你的输入。

相关问题