程序EMP(HR)上的PLS-00306错误

时间:2014-11-05 15:50:35

标签: oracle plsql cursor procedure

我的代码有问题..我不知道如何解决它:( .. 我需要使用UTL_FILE创建一个文件,输入如下所示:

Departamento: 80 - Sales
=========10========20========30========40========50========60=========
1234567890123456789012345678901234567890123456789012345678901234567890

EMPID         LAST_NAME     FIRST_NAME          SALARY           COMM
145           Russel        John               14000        0,4          


Departamento: 60 - IT

=======================================================================
EMPID         LAST_NAME     FIRST_NAME          SALARY           COMM
103           Hunold        Alexander               9000         NULO

etc...

我使用hr模式,我的程序编译没有问题:

CREATE OR REPLACE PROCEDURE EMP (p_ID IN hr.employees.employee_id%TYPE) AS
EPRINT NUMBER;
EPRINTRecord employees%ROWTYPE;
v_FileHandle UTL_FILE.FILE_TYPE;

CURSOR c_emp IS
 SELECT * FROM employees
 WHERE employee_id = p_ID;

BEGIN
 v_FileHandle := UTL_FILE.FOPEN('ASD', 'emp.txt', 'w');
 SELECT * INTO EPRINTRecord
 FROM employees
 WHERE department_id = p_ID;

 UTL_FILE.PUTF(v_FileHandle, 'Departamento: %s\n', EPRINTRecord.department_id);
 UTL_FILE.PUT_Line(v_FileHandle,'=========10========20========30========40========50========60=========');
 UTL_FILE.PUT_Line(v_FileHandle,'1234567890123456789012345678901234567890123456789012345678901234567890');
 UTL_FILE.PUTF(v_FileHandle, 'EMPID: %s\n', EPRINTRecord.employee_id, EPRINTRecord.last_name, EPRINTRecord.first_name, EPRINTRecord.salary, EPRINTRecord.commission_pct);
 UTL_FILE.FCLOSE(v_FileHandle);
 dbms_output.put_line('Escritura Correcta');

-- Manejo excepciones.
EXCEPTION
  WHEN UTL_FILE.INVALID_OPERATION THEN
  UTL_FILE.FCLOSE(v_FileHandle);
  RAISE_APPLICATION_ERROR(-20051, 'Error: Operacion no valida');
 WHEN UTL_FILE.INVALID_FILEHANDLE THEN
  UTL_FILE.FCLOSE(v_FileHandle);
  RAISE_APPLICATION_ERROR(-20052, 'Archivo no valido');
 WHEN UTL_FILE.READ_ERROR THEN
  UTL_FILE.FCLOSE(v_FileHandle);
  RAISE_APPLICATION_ERROR(-20053, 'Error de Lectura');
 WHEN UTL_FILE.INVALID_PATH THEN
  UTL_FILE.FCLOSE(v_FileHandle);
  RAISE_APPLICATION_ERROR(-20054, 'Directorio invalido');
 WHEN UTL_FILE.INVALID_MODE THEN
  UTL_FILE.FCLOSE(v_FileHandle);
  RAISE_APPLICATION_ERROR(-20055, 'Error: Modo invalido');
 WHEN UTL_FILE.INTERNAL_ERROR THEN
  UTL_FILE.FCLOSE(v_FileHandle);
  RAISE_APPLICATION_ERROR(-20056, 'Error interno DB');
 WHEN VALUE_ERROR THEN
  UTL_FILE.FCLOSE(v_FileHandle);
  RAISE_APPLICATION_ERROR(-20057, 'Error: Valor no valido');
 WHEN OTHERS THEN
  UTL_FILE.FCLOSE(v_FileHandle);
  RAISE;
END;
/

但是当我执行..

 exec emp;

SQL * PLUS说:

" ORA-06550:第1行,第7栏:

PLS-00306:调用&#E; EMP'中的参数数量或类型错误    ..."

如果我执行emp(1);文件已创建但仍为空。 请帮我!提前谢谢,对我的英语感到抱歉。 (我不知道它的好处)。

1 个答案:

答案 0 :(得分:0)

看起来您应该使用已定义的光标而不是SELECT...INTO...。尝试将您的程序重写为:

CREATE OR REPLACE PROCEDURE EMP (p_ID IN hr.employees.employee_id%TYPE) AS
  EPRINT NUMBER;
  v_FileHandle UTL_FILE.FILE_TYPE;

  CURSOR c_emp IS
    SELECT * FROM employees
    WHERE employee_id = p_ID;

BEGIN
  v_FileHandle := UTL_FILE.FOPEN('ASD', 'emp.txt', 'w');

  FOR ePrintRecord IN c_emp
  LOOP
    UTL_FILE.PUTF(v_FileHandle, 'Departamento: %s\n', EPRINTRecord.department_id);
    UTL_FILE.PUT_Line(v_FileHandle,
                      '=========10========20========30========40========50========60=========');
    UTL_FILE.PUT_Line(v_FileHandle,
                      '1234567890123456789012345678901234567890123456789012345678901234567890');
    UTL_FILE.PUTF(v_FileHandle, 'EMPID: %s\n', EPRINTRecord.employee_id);
  END LOOP;

  UTL_FILE.FCLOSE(v_FileHandle);
  dbms_output.put_line('Escritura Correcta');

-- Manejo excepciones.
EXCEPTION
  WHEN UTL_FILE.INVALID_OPERATION THEN
  UTL_FILE.FCLOSE(v_FileHandle);
  RAISE_APPLICATION_ERROR(-20051, 'Error: Operacion no valida');
 WHEN UTL_FILE.INVALID_FILEHANDLE THEN
  UTL_FILE.FCLOSE(v_FileHandle);
  RAISE_APPLICATION_ERROR(-20052, 'Archivo no valido');
 WHEN UTL_FILE.READ_ERROR THEN
  UTL_FILE.FCLOSE(v_FileHandle);
  RAISE_APPLICATION_ERROR(-20053, 'Error de Lectura');
 WHEN UTL_FILE.INVALID_PATH THEN
  UTL_FILE.FCLOSE(v_FileHandle);
  RAISE_APPLICATION_ERROR(-20054, 'Directorio invalido');
 WHEN UTL_FILE.INVALID_MODE THEN
  UTL_FILE.FCLOSE(v_FileHandle);
  RAISE_APPLICATION_ERROR(-20055, 'Error: Modo invalido');
 WHEN UTL_FILE.INTERNAL_ERROR THEN
  UTL_FILE.FCLOSE(v_FileHandle);
  RAISE_APPLICATION_ERROR(-20056, 'Error interno DB');
 WHEN VALUE_ERROR THEN
  UTL_FILE.FCLOSE(v_FileHandle);
  RAISE_APPLICATION_ERROR(-20057, 'Error: Valor no valido');
 WHEN OTHERS THEN
  UTL_FILE.FCLOSE(v_FileHandle);
  RAISE;
END;
/

祝你好运。