1

我正在做学校作业,我的代码有错误。我创建了一个 PLSQL 包:

create or replace 
PACKAGE GestionProjets AS

  TYPE Signaletique IS TABLE OF EMPLOYES%ROWTYPE INDEX BY BINARY_INTEGER; 
  TYPE TableNomDep IS TABLE OF VARCHAR2(40);

  PROCEDURE AjouterProjets(ProjetRecord IN PROJETS%ROWTYPE);
  PROCEDURE SupprimerProjet(DelNumPro IN PROJETS.numpro%TYPE);
  --PROCEDURE ModifierProjet(newRecord IN PROJETS%ROWTYPE, oldRecord IN PROJETS%ROWTYPE);
  FUNCTION ListerEmployes(dep1 IN DEPARTEMENTS.Nomdep%TYPE, dep2 IN DEPARTEMENTS.Nomdep%TYPE) RETURN Signaletique;

END GestionProjets;

那是我有错误的函数的主体:

  FUNCTION ListerEmployes(dep1 IN DEPARTEMENTS.Nomdep%TYPE, dep2 IN DEPARTEMENTS.Nomdep%TYPE) RETURN Signaletique AS

    nomDeps TableNomDep;
    SigTable Signaletique;
    EXWrongDep1 EXCEPTION;
    EXWrongDep2 EXCEPTION;

  BEGIN

    SELECT Nomdep 
    BULK COLLECT INTO nomDeps
    FROM DEPARTEMENTS;

    -- test if dep 1 est un parametre valide
    IF NOT nomDeps.exists(dep1) THEN
      RAISE EXWrongDep1;
    END IF;

    -- test if dep 2 est un parametre valide
    IF NOT nomDeps.exists(dep2) THEN
      RAISE EXWrongDep2;
    END IF;




    EXCEPTION
      WHEN EXWrongDep1 THEN
        RAISE_APPLICATION_ERROR(-20008, 'MAUVAIS PARAMETRE: ' || dep1 || ' N EXISTE PAS!');
      WHEN EXWrongDep2 THEN
        RAISE_APPLICATION_ERROR(-20008, 'MAUVAIS PARAMETRE: ' || dep2 || ' N EXISTE PAS!');
            WHEN OTHERS THEN
                RAISE_APPLICATION_ERROR (-20007, 'ERREUR INCONNU: ' ||  SQLCODE || ' - ' || SQLERRM);

  END ListerEmployes;

当我尝试执行该函数时,我收到以下错误消息:

DECLARE
  tabel gestionprojets.Signaletique;
BEGIN
  tabel := gestionprojets.listeremployes('sdsd','sdsd');
END;


ORA-20007: ERREUR INCONNU: -6502 - ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at "EDGE.GESTIONPROJETS", line 108
ORA-06512: at line 4

我不明白为什么会收到此错误消息。Nomdep 列的类型是 VARCHAR2(40)。

4

1 回答 1

3

我怀疑问题是表类型是由 BINARY_INTEGER 索引的。当.EXISTS调用被执行时,它们要么作为表的索引传递,要么dep1作为dep2表的索引传递,因此 PL/SQL 尝试将 VARCHAR2(40) 参数转换为 BINARY_INTEGER。由于这些输入参数都是非数字字符串 ('sdsd'),因此转换失败。

DEPARTEMENTS如果不是每次ListerEmployes调用都将整个表读入内存,而是将其重写为,则此代码可能会运行得更快

FUNCTION ListerEmployes(dep1 IN DEPARTEMENTS.Nomdep%TYPE, dep2 IN DEPARTEMENTS.Nomdep%TYPE) RETURN Signaletique
AS
  strNomdep  DEPARTEMENTS.Nomdp%TYPE;
BEGIN
  -- test if dep 1 est un parametre valide

  BEGIN
    SELECT NOMDEP
      INTO strNomdep
      FROM DEPARTEMENTS
      WHERE NOMDEP = dep1;
  EXCEPTION
    WHEN NO_DATA_FOUND THEN
      RAISE_APPLICATION_ERROR(-20008, 'MAUVAIS PARAMETRE: ' || dep1 || ' N EXISTE PAS!'); 
  END;

  -- test if dep 2 est un parametre valide

  BEGIN
    SELECT NOMDEP
      INTO strNomdep
      FROM DEPARTEMENTS
      WHERE NOMDEP = dep2;
  EXCEPTION
    WHEN NO_DATA_FOUND THEN
      RAISE_APPLICATION_ERROR(-20008, 'MAUVAIS PARAMETRE: ' || dep2 || ' N EXISTE PAS!'); 
  END;
  EXCEPTION 
    WHEN OTHERS THEN 
      RAISE_APPLICATION_ERROR (-20007, 'ERREUR INCONNU: ' ||  SQLCODE || ' - ' || SQLERRM);    
END ListerEmployes;

分享和享受。

于 2012-08-15T17:04:00.927 回答