获取 PL/SQL:数字或值错误:字符到数字的转换错误
Posted
技术标签:
【中文标题】获取 PL/SQL:数字或值错误:字符到数字的转换错误【英文标题】:Getting PL/SQL: numeric or value error: character to number conversion error 【发布时间】:2011-12-16 18:27:53 【问题描述】:我收到此错误:
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;
错误发生在带有 ** 的行上。我不太确定是什么导致了这个错误,有什么帮助吗?
【问题讨论】:
【参考方案1】:鉴于您的 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”被声明为:
CriteriaSequenceId NUMBER := 1;
因此,您的函数期望 VARCHAR2 作为第一个参数,但您向它发送了一个数字。从外观上看,您的参数顺序错误。
【讨论】:
【参考方案2】:为了维护SP的参数顺序和为SP调用的函数,两个参数顺序必须相同。
例如: 如果您的 SP 的第一个参数是 @p_ID,那么您的函数调用的第一个参数必须是 p_ID
【讨论】:
【参考方案3】:请使用此语法,
CriteriaId := InsertSrchCriteria
(
SearchMethod =>test1,
CritExpression =>test2,
SubstringStartPosition =>null,
SubstringLength =>test3,
CritDescription =>...,
CriteriaSequenceId =>...,
SearchId =>..
);
您将确定每个变量的去向和 以后更容易阅读代码.. ;) –
【讨论】:
以上是关于获取 PL/SQL:数字或值错误:字符到数字的转换错误的主要内容,如果未能解决你的问题,请参考以下文章
执行立即错误:PL/SQL:数字或值错误:字符到数字转换错误
ORA-06502: PL/SQL: 数字或值错误: 字符到数字的转换错误
ORA-06502 PL/SQL:数字或值错误:字符到数字的转换错误;
Oracle ORA-06502 PL/SQL:数字或值错误:字符到数字的转换错误