0

我需要一些帮助。这是我第一次在这里寻求帮助,所以请原谅我的英语不好。我很生气,因为我不知道如何解决这个错误。我在Oracle 10g. 输出是:

Function created.
No errors.
Function created.
Enter value for v_numes: 'SECTIA1'  --SECTIA1 is the name of section(department) from sectii table
Sectia exista in baza de date SECTIA1
Numele sectiei este: SECTIA1 --the name of the department(v_numes)
DECLARE
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at "SYSTEM.FUNCTION0", line18
ORA-01403: no data found
ORA-06512: at line 17

这是我的代码:

set serveroutput on 
set verify off
CREATE OR REPLACE FUNCTION function0(p_section_name VARCHAR) RETURN NUMBER IS
    v_procent1  CONSTANT REAL:=0.01;
    v_cods      sectii.cods%TYPE;--the table is named sectii, which means sections
    v_ok1       NUMBER:=1;
BEGIN
    DBMS_OUTPUT.PUT_LINE('Numele sectiei este: '||p_section_name);
    SELECT cods
    INTO v_cods
    FROM sectii
    WHERE den=UPPER(p_section_name);
    DBMS_OUTPUT.PUT_LINE('Codul sectiei pentru '||p_section_name||' este '||v_cods);

    RETURN (v_ok1);
EXCEPTION
    WHEN TOO_MANY_ROWS THEN
        RETURN 'Prea multe sectii cu acest nume.';
    WHEN NO_DATA_FOUND THEN
        RETURN 'Nici o sectie cu acest nume.';
    WHEN OTHERS THEN
        RAISE_APPLICATION_ERROR(-20011,'Unknown Exception in function0');
END function0;
/
show errors;
/
DECLARE
    v_numes             VARCHAR2(255);
    v_numesu            VARCHAR2(255);
    v_numesl            VARCHAR2(255);
    v_boool             NUMBER:=1;
    e_invalid_section   EXCEPTION;
BEGIN
    v_numes:=&v_numes;
    IF SQL%NOTFOUND THEN
        RAISE e_invalid_section;
    ELSE
        DBMS_OUTPUT.PUT_LINE('Sectia exista in baza de date '|| v_numes);
    END IF;
    v_numesu:=UPPER(v_numes);
    v_numesl:=LOWER(v_numes);
    IF v_numes=v_numesl OR v_numes=v_numesu THEN
        DBMS_OUTPUT.PUT_LINE('FUNCTION0 '||function0(v_numes));
    END IF; 
    --v_numes:=TO_CHAR(v_numes);
    --DBMS_OUTPUT.PUT_LINE('RETURN FUNCTIE0: '||v_boool);
EXCEPTION
    WHEN e_invalid_section THEN --exceptie definita de utilizator
        DBMS_OUTPUT.PUT_LINE('Nu exista aceasta sectie in tabelul sectii.');    
END;
/
4

1 回答 1

4

, 的声明FUNCTION0表明该函数返回一个数字:

CREATE OR REPLACE FUNCTION function0(p_section_name VARCHAR) RETURN NUMBER IS

异常块中的返回语句返回字符...

EXCEPTION
    WHEN TOO_MANY_ROWS THEN
        RETURN 'Prea multe sectii cu acest nume.';
    WHEN NO_DATA_FOUND THEN
        RETURN 'Nici o sectie cu acest nume.';

需要注意的一件事是,您的错误非常具体:

ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at "SYSTEM.FUNCTION0", line18
ORA-01403: no data found
ORA-06512: at line 17

这意味着您在第 18 行将字符转换为数字,并且在第 17 行引发了 NO_DATA_FOUND 异常...

正如我已经说过的,要在一个地方回答您的所有评论,由于 WHERE 子句,您会得到一个 NO_DATA_FOUND 异常。

WHERE den = UPPER(p_section_name)

运行此 SELECT 语句:

select * from cods where den = upper(den)

它不会返回任何行,因为它不正确。如果您将参数大写,则p_section_name还需要将 column 大写den。您需要进行不区分大小写的搜索,有关更多详细信息,请参阅Oracle 中不区分大小写的搜索。

于 2013-06-22T09:49:48.980 回答