用户定义的函数,PL / SQL Oracle中的IN VARCHAR参数

时间:2013-06-22 09:38:27

标签: sql plsql oracle10g user-defined-functions

我需要一些帮助。这是我第一次在这里寻求帮助,请原谅我的英语不好。我很生气,因为我无法弄清楚如何解决这个错误。我在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;
/

1 个答案:

答案 0 :(得分:3)

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

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

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

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置于上限,则还需要将列den置为大写。您需要不区分大小写搜索,有关详细信息,请参阅Case insensitive searching in Oracle