如何使用EXECUTE关键字使用参数执行PL / SQL存储过程

时间:2017-12-16 16:26:59

标签: oracle stored-procedures plsql

以下示例创建一个存储过程,该过程查找两个数字之间的最小值并打印出最小值:

create or replace procedure findMin(x IN number, y IN number) IS
BEGIN
   IF x < y THEN
      dbms_output.put_line(x||' is the smallest number.');
   ELSE
      dbms_output.put_line(y||' is the smallest number.');
   END IF;
END; 

执行匿名PL / SQL块内部的过程不会产生任何问题:

begin
findMin(5,10);
end;

但是,匿名块之外的以下代码不起作用:

execute findMin(5,10);

当这些过程需要参数时,如何使用execute命令执行存储的PL / SQL过程?这甚至可能吗?

1 个答案:

答案 0 :(得分:1)

不确定你的意思是“不起作用” - 这是一个简短会话(在SQL * Plus中)的屏幕截图,以证明它的工作正常。

两个注释:execute是一个SQL * Plus命令,所以它不需要以分号结束(分号不应该伤害任何东西,但它只是不需要);并且,为了能够看到输出,您必须首先执行SQL * Plus命令set serveroutput on。如果“不起作用”只是意味着“我看不到输出”,那么也许你错过了这一步。 (我的插图中没有显示,因为我将系统设置为默认启用服务器输出。)

SQL> create or replace procedure findMin(x IN number, y IN number) IS
  2  BEGIN
  3     IF x < y THEN
  4        dbms_output.put_line(x||' is the smallest number.');
  5     ELSE
  6        dbms_output.put_line(y||' is the smallest number.');
  7     END IF;
  8  END;
  9  /

Procedure created.

Elapsed: 00:00:00.09
SQL> execute findMin(5, 10)
5 is the smallest number.

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.00
SQL>
相关问题