为什么我得到这个:PLSQL字符串长度约束错误

时间:2017-09-08 06:41:06

标签: sql oracle plsql

我现在正在研究这段代码,并尝试了我能想到的一切。

这是代码块:

CREATE OR REPLACE PROCEDURE ADD_LOCATION_TO_DB (ploccode VARCHAR2, pminqty 
NUMBER, pmaxqty NUMBER)AS
    Err_Locode_Length EXCEPTION;
    Err_Minqty_Range EXCEPTION;
    Err_Maxqty_Range EXCEPTION;
    Err_Maxqty_Greater_Minqty EXCEPTION;
BEGIN
    IF LENGTH(ploccode) != 5 THEN
        RAISE Err_Locode_Length;
    ELSE IF pminqty > 10 OR pminqty < 0 THEN
        RAISE Err_Minqty_Range;
    ELSE IF pmaxqty > 10 OR pmaxqty < 0 THEN
        RAISE Err_Maxqty_Range;
    ELSE IF pminqty > pmaxqty THEN
        RAISE Err_Maxqty_Greater_Minqty;
    END IF;
    END IF;
    END IF;
    END IF;
INSERT INTO LOCATION
VALUES (ploccode, pminqty, pmaxqty);
EXCEPTION
    WHEN DUP_VAL_ON_INDEX THEN
        RAISE_APPLICATION_ERROR(-20081,'Duplicate Location ID');
    WHEN Err_Locode_Length THEN
        RAISE_APPLICATION_ERROR(-20082,'Location Code Lenght invalid');
    WHEN Err_Minqty_Range THEN
        RAISE_APPLICATION_ERROR(-20083,'Minium Qty is out of range');
    WHEN Err_Maxqty_Range THEN
        RAISE_APPLICATION_ERROR(-20084,'Maximum Qty is out of range');
    WHEN Err_Maxqty_Greater_Minqty THEN
        RAISE_APPLICATION_ERROR(-20086,'Minium Qty is Lager than Maximum Qty');
    WHEN OTHERS THEN
        RAISE_APPLICATION_ERROR(-20000,'Use Value of SQLERRM');
END;
/
CREATE OR REPLACE PROCEDURE ADD_LOCATION_VIASQLDEV AS
    Dis_Msg VARCHAR2;
    ploccode VARCHAR2;
    pminqty NUMBER;
    pmaxqty NUMBER;
BEGIN
    dbms_output.put_line('--------------------------------------------------');
    dbms_output.put_line('Adding location  LocCode: ' || ploccode || ' MinQty: ' || pminqty || 'MaxQty' || pmaxqty);
    ADD_LOCATION_TO_DB(ploccode, pminqty, pmaxqty);
    dbms_output.put_line(Dis_Msg);
END;
/

当我运行这组程序时,我在编译器中收到错误说明:

  

错误(1,13):PLS-00215:字符串长度约束必须在范围内(1 ... 32767)

     

错误(2,14):PLS-00215:字符串长度约束必须在范围内(1 ... 32767)

我尝试在调用主过程的代码中添加约束:

CREATE OR REPLACE PROCEDURE ADD_LOCATION_VIASQLDEV AS
    Dis_Msg VARCHAR2(40);
    ploccode VARCHAR2(5);
    pminqty NUMBER(2);
    pmaxqty NUMBER(2);
BEGIN
    dbms_output.put_line('--------------------------------------------------');
    dbms_output.put_line('Adding location  LocCode: ' || ploccode || ' MinQty: ' || pminqty || 'MaxQty' || pmaxqty);
    ADD_LOCATION_TO_DB(ploccode, pminqty, pmaxqty);
    dbms_output.put_line(Dis_Msg);
END;
/

添加这些约束确实会删除编译器的错误,但是在执行此测试代码时仍然会重现错误:

Error starting at line : 48 in command -
begin
dbms_output.put_line('Student ID: 1234567');
dbms_output.put_line('==========PART 3 TEST LOCATIONS==========================');
ADD_LOCATION_VIASQLDEV ('AF201',1,2);
ADD_LOCATION_VIASQLDEV('AF202',-3,4);
ADD_LOCATION_VIASQLDEV ('AF203',5,1);
ADD_LOCATION_VIASQLDEV ('AF204',6,7000);
ADD_LOCATION_VIASQLDEV ('AF20111',8,9);
end;
Error report -
ORA-06550: line 4, column 1:
PLS-00306: wrong number or types of arguments in call to 'ADD_LOCATION_VIASQLDEV'
ORA-06550: line 4, column 1:
PL/SQL: Statement ignored
ORA-06550: line 5, column 1:
PLS-00306: wrong number or types of arguments in call to 'ADD_LOCATION_VIASQLDEV'
ORA-06550: line 5, column 1:
PL/SQL: Statement ignored
ORA-06550: line 6, column 1:
PLS-00306: wrong number or types of arguments in call to 'ADD_LOCATION_VIASQLDEV'
ORA-06550: line 6, column 1:
PL/SQL: Statement ignored
ORA-06550: line 7, column 1:
PLS-00306: wrong number or types of arguments in call to 'ADD_LOCATION_VIASQLDEV'
ORA-06550: line 7, column 1:
PL/SQL: Statement ignored
ORA-06550: line 8, column 1:
PLS-00306: wrong number or types of arguments in call to 'ADD_LOCATION_VIASQLDEV'
ORA-06550: line 8, column 1:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

有人可以告诉我我做错了什么。

如果这是有太多信息或我的格式化错误,请道歉。

2 个答案:

答案 0 :(得分:2)

在第一组错误中,您将函数声明为使用VARCHAR2变量,但未在括号中声明长度。陈述一个长度

在你的第二组错误中,你调用函数add_location_viasqldev,该函数接受0个参数,但你提供了3.提供另一个参数或调用另一个函数

另外,你可以通过这样做来使你的if / else更整洁:

IF test
ELSIF test
ELSIF test
ELSE
END IF;

答案 1 :(得分:1)

您对程序的声明部分和参数部分感到困惑。

  • 您的第一个错误是由于您将所谓的参数放在声明部分中。然后当你试图为它添加长度时,它编译了,但是你没有参数就离开了程序。那么你用四个参数运行它会导致错误,因为创建的过程不接受任何参数。

    create or replace procedure_name(
                                     parameter1 datatype, --arguments section
                                     parameter2 datatype
                                     ) AS
    /*declaration section*/
    variable1 datatype(length);
    variable2 datatype(length);
    BEGIN
    
    /*your code here*/
    
    END;
    

试试这个修改脚本

CREATE OR REPLACE PROCEDURE ADD_LOCATION_VIASQLDEV (Dis_Msg VARCHAR2,
                                                    ploccode VARCHAR2,
                                                    pminqty NUMBER,
                                                    pmaxqty NUMBER ) AS 
BEGIN
dbms_output.put_line('--------------------------------------------------');
dbms_output.put_line('Adding location  LocCode: ' || ploccode || ' MinQty: ' || pminqty || 'MaxQty' || pmaxqty);
ADD_LOCATION_TO_DB(ploccode, pminqty, pmaxqty);
dbms_output.put_line(Dis_Msg);
END;