获取PL / SQL:数字或值错误:字符到数字转换错误

时间:2011-12-16 18:27:53

标签: oracle plsql

我收到此错误:

ORA-06502:PL / SQL:数字或值错误:字符到数字转换错误 ORA-06512:第146行 06502. 00000 - " PL / SQL:数字或值错误%s"

这是我的代码:

   SET SERVEROUTPUT ON;

DECLARE
    SearchId NUMBER := 0;
    SearchMethod VARCHAR2(100) := '';
    CritExpression VARCHAR2(100) := '';
    SubstringStartPosition NUMBER := 0;
    SubstringLength NUMBER := 0;
    CritDescription VARCHAR2(100) := '';
    CriteriaSequenceId NUMBER := 1;
    CriteriaId NUMBER := 0;
    CritCount NUMBER := 0;

FUNCTION InsertSrchCriteria
(
    SearchMethod IN VARCHAR2,
    CritExpression IN VARCHAR2,
    SubstringStartPosition IN NUMBER,
    SubstringLength IN NUMBER,
    CritDescription IN VARCHAR2,
    CriteriaSequenceId IN NUMBER,
    SearchId IN NUMBER
)
    RETURN NUMBER
IS
    C_Id NUMBER := 0;
BEGIN
    SELECT COUNT (*) INTO CritCount FROM criteria_table WHERE search_id = SearchId AND criteria_sequence_id = CriteriaSequenceId;

    IF CritCount = 0
    THEN
        INSERT INTO criteria_table
            (
                criteria_sequence_id,
                search_id,
                search_method,
                expression,
                substring_start_position,
                substring_length,
                description
            )
            VALUES
            (
                CriteriaSequenceId,
                SearchId,
                SearchMethod,
                CritExpression,
                SubstringStartPosition,
                SubstringLength,
                CritDescription
            )
            RETURNING criteria_id INTO C_Id;
        IF C_Id > 0
        THEN
            DBMS_OUTPUT.PUT_LINE ('Inserted ' || 'SearchId: ' || SearchId || ' @' || CriteriaSequenceId || ' successfully');
        ELSE
            DBMS_OUTPUT.PUT_LINE ('Not Inserted ' || 'SearchId: ' || SearchId || ' @' || CriteriaSequenceId);
        END IF;
    ELSE
        DBMS_OUTPUT.PUT_LINE ('Already exists ' || 'SearchId: ' || SearchId || ' @' || CriteriaSequenceId);
    END IF;
    RETURN C_Id;
END InsertSrchCriteria;

BEGIN
        SearchId = 5;
        CriteriaSequenceId := 1;
        SearchMethod := 'XPath';
        CritExpression := '//Expression/text()';
        SubstringStartPosition := null;
        SubstringLength := null;
        CritDescription := '';

        CriteriaId := InsertSrchCriteria  ****
        (
            CriteriaSequenceId,
            SearchId,
            SearchMethod,
            CritExpression,
            SubstringStartPosition,
            SubstringLength,
            CritDescription
        );
END;

错误发生在 * *的行上。我不太确定是什么导致了这个错误,有什么帮助吗?

3 个答案:

答案 0 :(得分:6)

根据你的FUNCTION定义:

FUNCTION InsertSrchCriteria
(
    SearchMethod IN VARCHAR2,
    CritExpression IN VARCHAR2,
    SubstringStartPosition IN NUMBER,
    SubstringLength IN NUMBER,
    CritDescription IN VARCHAR2,
    CriteriaSequenceId IN NUMBER,
    SearchId IN NUMBER
)

然后你如何调用InsertSrchCriteria:

    CriteriaId := InsertSrchCriteria  ****
    (
        CriteriaSequenceId,
        SearchId,
        SearchMethod,
        CritExpression,
        SubstringStartPosition,
        SubstringLength,
        CritDescription
    );

您的第一个参数“CriteriaSequenceId”已DECLAREd为:

CriteriaSequenceId NUMBER := 1;

所以你的函数期望VARCHAR2作为第一个参数,但是你要发送一个数字。从它的外观来看,你的参数只是错误的顺序。

答案 1 :(得分:1)

确保维护SP的参数顺序和调用SP的函数,两个参数顺序必须相同。

例如: 如果您的 SP 的第一个参数是 @p_ID,那么您的函数调用第一个参数必须是 p_ID

答案 2 :(得分:0)

请使用此语法,

 CriteriaId := InsertSrchCriteria
(
    SearchMethod =>test1,
    CritExpression =>test2,
    SubstringStartPosition =>null,
    SubstringLength =>test3,
    CritDescription =>...,
    CriteriaSequenceId =>...,
    SearchId =>..
);

您将确定每个变量的位置和位置  以后更容易阅读代码..;) -