【问题标题】:PLS-00306: wrong number or types of arguments in function callPLS-00306:函数调用中的参数数量或类型错误
【发布时间】:2015-03-08 15:38:05
【问题描述】:

我不知道如何在 SQL Developer 中调用函数。我正在尝试调用函数 GET_SUIT,但它说我在调用“GET_SUIT”时使用了错误数量或类型的参数:

create or replace FUNCTION GET_SUIT 
(
  RND_NUM IN INTEGER, 
  Suit OUT VARCHAR2 
) RETURN VARCHAR2 AS 
BEGIN

if RND_NUM = 1 then -- Card is a Spade
  Suit := 'Spades';

elsif RND_NUM = 2 then  -- Card is a Heart
  Suit := 'Hearts';

elsif RND_NUM = 3 then -- Card is a Diamond
  Suit := 'Diamonds';

elsif RND_NUM = 4 then -- Card is an Club
  Suit := 'Clubs';

end if;

RETURN Suit;
END GET_SUIT;

我正在使用以下语句:

 SELECT dbms_random.value(1,4) into RND_NUM from dual;
 dbms_output.put_line('Random number 2 is : ' || RND_NUM);
 GET_SUIT(RND_NUM);
 dbms_output.put_line('Suit of card is : ' || Suit);   

从我读过的所有内容中,我需要将一个整数传递给函数,对吗?任何帮助将不胜感激,我知道这是基本级别的东西,但我还有其他我想使用的功能,我什至无法让这个简单的功能工作。

【问题讨论】:

    标签: oracle function plsql call


    【解决方案1】:

    您收到错误消息有两个原因。

    一个,因为你的函数有两个参数,但你在调用中只分配了一个。您缺少用于接收 OUT 参数的局部变量。

    二,因为函数返回一个值。因此,调用必须分配给局部变量;或者,我们可以在 SELECT 语句的投影中使用函数。这也意味着我们不在函数签名中使用 OUT 参数(我们可以,函数仍然可以编译,但这是不好的做法)。

    所以,这样写你的函数...

    create or replace FUNCTION GET_SUIT 
    (
      RND_NUM IN INTEGER
    ) RETURN VARCHAR2 
    AS 
      Suit  VARCHAR2(10); 
    BEGIN
    
    if RND_NUM = 1 then -- Card is a Spade
      Suit := 'Spades';
    
    elsif RND_NUM = 2 then  -- Card is a Heart
      Suit := 'Hearts';
    
    elsif RND_NUM = 3 then -- Card is a Diamond
      Suit := 'Diamonds';
    
    elsif RND_NUM = 4 then -- Card is an Club
      Suit := 'Clubs';
    
    end if;
    
    RETURN Suit;
    END GET_SUIT;
    

    ...并这样称呼它:

    declare
        l_suit varchar2(10);
        rnd_num pls_integer;
    begin
         SELECT dbms_random.value(1,4) into RND_NUM from dual;
         dbms_output.put_line('Random number 2 is : ' || RND_NUM);
         l_suit := GET_SUIT(RND_NUM);
         dbms_output.put_line('Suit of card is : ' || l_Suit);   
    end;
    /
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-22
      • 2014-10-01
      相关资源
      最近更新 更多