有没有办法将默认参数添加到具有 input/output 参数的 sql 过程?

Is there a way to add default params to sql procedure that has input/output params?

我有这个代码:

set serveroutput on;

CREATE OR REPLACE PROCEDURE myProc(
            id IN NUMBER, 
            optional_txt IN VARCHAR2 DEFAULT NULL, 
            random_pct OUT NUMBER
            )
    IS BEGIN
        random_pct := 101;      
    END myProc;

我只想使用所需的输入参数 (id) 调用此过程,如下所示:

myProc(id, random_pct);

但是我得到这个错误:PLS-00306: wrong number or types of arguments in call to 'myProc'

如果我删除输出参数,它会正常工作,如下所示:

set serveroutput on;

CREATE OR REPLACE PROCEDURE myProc(
            pn_random_id IN NUMBER, 
            pn_optional_txt IN VARCHAR2 DEFAULT NULL
            )
    IS BEGIN
        dbms_output.put_line('Proc created.');
    END myProc;

(我是这样叫的):

myProc(id);

如果我还需要输出参数,我该如何完成这项工作?

创建一个函数而不是过程

CREATE OR REPLACE function myfunction(
            pn_random_id IN NUMBER, 
            pn_optional_txt IN VARCHAR2 DEFAULT NULL
            ) return NUMBER
    IS BEGIN
        dbms_output.put_line('Proc created.');
      return  1; -- return value you need
    END myProc;

你怎么称呼它

declare
  v_result number;
begin
  v_result := myfunction(1);
end;
/

好吧,函数而不是过程是更好的解决方案。但是您可以使用您的原始过程并仅使用两个参数进行调用。但是您需要将调用更改为命名参数而不是位置参数。

create or replace 
procedure myproc(
          id            in number 
        , optional_txt  in varchar2 default null 
        , random_pct   out number
        )
is 
begin
    random_pct := 101+id;      
end myProc;


declare 
   res  number;
begin
   myproc ( id         => 1
          , random_pct => res
          ); 
   dbms_output.put_line('res=' || to_char(res));
end;    

or even

declare 
   res  number;
begin
   myproc ( random_pct => res
          , id         => 2
          ); 
   dbms_output.put_line('res=' || to_char(res));
end;