PLS-00201:标识符' V_OUT_FILE'必须申报

时间:2017-04-26 12:22:27

标签: oracle plsql

我正在尝试将一些字符串写入txt文件,但它告诉我必须声明V_OUT_FILE但我已经在块的声明部分声明了它。有什么建议? 这是完整的块:

CREATE OR REPLACE DIRECTORY CTEST AS 'C:\Users\myUser\Desktop';
GRANT WRITE ON DIRECTORY CTEST TO PUBLIC

DECLARE
  v_out_File  UTL_FILE.FILE_TYPE;
BEGIN
  v_out_File := UTL_FILE.FOPEN('CTEST', 'IO.txt' , 'W');

  UTL_FILE.PUT_LINE(v_out_File , 'Hi this is text file! \n');
  UTL_FILE.PUT_LINE(v_out_File , 'Hi this is line 2! \n');
  UTL_FILE.FCLOSE(v_out_File);
END;

1 个答案:

答案 0 :(得分:4)

问题实际上是在grant语句结束时缺少分号。抛出ORA-00933,你没有提到过。因为该语句没有正确结束,所以DECLARE以及在声明结束时第一个分号的所有内容都被视为该授权的一部分,您可以在错误报告中看到(来自SQL Developer) ):

Directory CTEST created.


Error starting at line : 2 in command -
GRANT WRITE ON DIRECTORY CTEST TO PUBLIC

DECLARE
  v_out_File  UTL_FILE.FILE_TYPE
Error report -
ORA-00933: SQL command not properly ended
00933. 00000 -  "SQL command not properly ended"
*Cause:    
*Action:

这意味着nest语句从BEGIN开始,就编译器而言实际上必须是DECLARE部分。再次出现在错误报告中:

Error starting at line : 6 in command -
BEGIN
  v_out_File := UTL_FILE.FOPEN('CTEST', 'IO.txt' , 'W');

  UTL_FILE.PUT_LINE(v_out_File , 'Hi this is text file! \n');
  UTL_FILE.PUT_LINE(v_out_File , 'Hi this is line 2! \n');
  UTL_FILE.FCLOSE(v_out_File);
END;
Error report -
ORA-06550: line 2, column 3:
PLS-00201: identifier 'V_OUT_FILE' must be declared
...

如果添加缺少的分号,则可以正常工作:

CREATE OR REPLACE DIRECTORY CTEST AS 'C:\Users\myUser\Desktop';
GRANT WRITE ON DIRECTORY CTEST TO PUBLIC;

DECLARE
  v_out_File  UTL_FILE.FILE_TYPE;
BEGIN
  v_out_File := UTL_FILE.FOPEN('CTEST', 'IO.txt' , 'W');

  UTL_FILE.PUT_LINE(v_out_File , 'Hi this is text file! \n');
  UTL_FILE.PUT_LINE(v_out_File , 'Hi this is line 2! \n');
  UTL_FILE.FCLOSE(v_out_File);
END;
/

得到

Directory CTEST created.


Grant succeeded.


PL/SQL procedure successfully completed.