为什么在plsql函数中出现字符串长度错误

时间:2018-09-30 11:00:57

标签: sql oracle plsql

我制作了一个需要在参数内添加日期的函数。它应该返回varchar2:'Weekend'或'Doordeweeks'

SET SERVEROUTPUT ON;
declare
    resultaat varchar2;
    datum date;
    function is_weekend(p_datum in varchar2)
    return varchar2
    is
    begin
        datum := to_date(p_datum, 'dd-mm-yyyy');
        if (to_char(datum, 'd') in ('1', '7')) then
            resultaat := 'Weekend';
            dbms_output.put_line(resultaat);
            return resultaat;
        else
            resultaat := 'Doordeweeks';
            dbms_output.put_line(resultaat);
            return resultaat;

        end if;
    end is_weekend;
    begin
        dbms_output.put_line(is_weekend('30-09-2018'));
    end;

运行此代码会使我得到此pl / sql编译错误:

Error report - ORA-06550: line 2, column 15: PLS-00215: String length constraints must be in range (1 .. 32767)
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

好像出了什么问题,有人知道导致此错误的原因吗?

1 个答案:

答案 0 :(得分:0)

您只需要设置varhcar2的大小即可,并且不需要使用to_date将其分配给dateu,如下所示: 样本输入:

SET SERVEROUTPUT ON;
declare       
    resultaat varchar2(20);
    datum date;
    function is_weekend(p_datum in varchar2)
    return varchar2
    is
    begin
        datum := p_datum;
        if (to_char(datum, 'd') in ('1', '7')) then
            resultaat := 'Weekend';
            dbms_output.put_line(resultaat);
            return resultaat;
        else
            resultaat := 'Doordeweeks';
            dbms_output.put_line(resultaat);
            return resultaat;

        end if;
    end is_weekend;
    begin
        dbms_output.put_line(is_weekend('30-09-2018'));
    end;

示例输出:

Weekend
Weekend

begin
        dbms_output.put_line(is_weekend('30-nov-2018'));
    end;

Doordeweeks
Doordeweeks