是否可以将默认参数添加到具有输入/输出参数的sql过程中?

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

我有此代码:

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);

如果还需要输出参数,该如何进行这项工作?

sql oracle default optional-parameters default-parameters
2个回答
0
投票

创建函数而不是过程

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;
/

0
投票

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

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;   
© www.soinside.com 2019 - 2024. All rights reserved.