0

I want to read text file, store it in a variable and output the variable in pl/sql. I am using the following code but getting error:statement ignored.

create or replace PROCEDURE blah
AS
DECLARE
f UTL_FILE.FILE_TYPE;
s VARCHAR2(200);
BEGIN
f := UTL_FILE.FOPEN('C:\Users\s420105\Music','blahh.txt','R');
UTL_FILE.GET_LINE(f,s);
UTL_FILE.FCLOSE(f);
dbms_output.put_line(s);
END;

I am not sure about UTL permission but i suppose it will throw different error if it is due to UTL permission. The

0

1 Answer 1

5

Several issues:

  • You don't need the DECLARE statement. The syntax is CREATE [OR REPLACE] PROCEDURE [schema.]name AS ... BEGIN ... END;
  • The location argument for UTL_FILE.FOPEN is a handle to a directory object not the a string containing the directory path.

This code should work:

CREATE DIRECTORY DIR__MUSIC AS 'C:/Users/s420105/Music';
/

CREATE PROCEDURE blah
AS
  f UTL_FILE.FILE_TYPE;
  s VARCHAR2(200);
BEGIN
  f := UTL_FILE.FOPEN( 'DIR__MUSIC', 'blahh.txt', 'R', 200 );
  UTL_FILE.GET_LINE( f, s );
  UTL_FILE.FCLOSE( f );
  DBMS_OUTPUT.PUT_LINE( s );
END;
/
SHOW ERRORS;
/

One simple way to read the entire file is:

CREATE DIRECTORY DIR__MUSIC AS 'C:/Users/s420105/Music';
/

CREATE PROCEDURE blah
AS
  f CLOB;
BEGIN
  f := DBMS_XSLPROCESSOR.READ2CLOB( 'DIR__MUSIC', 'blahh.txt' );
  DBMS_OUTPUT.PUT_LINE( f );
END;
/
SHOW ERRORS;
/
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the explanation. While your code is compiled without error when i tried executing it i am getting runt time error invalid file operation and for the previous code had error: numeric or value error character to number conversion error.
What is the full error message incluing ORA- number? Also, has the user (who is compiling/running the procedure) got read permissions on the directory? Try GRANT READ ON DIRECTORY dir__music TO username; (from an appropriately priviledged user).
It a local directory, dont think there is any permissions issue. The error details are:ORA-29283: invalid file operation ORA-06512: at "SYS.UTL_FILE", line 536 ORA-29283: invalid file operation ORA-06512: at "XDB.DBMS_XSLPROCESSOR", line 265 ORA-06512: at "d743721.BLAH", line 5 ORA-06512: at line 2
This is not an issue with the SQL but an issue of permissions. You need to check that (a) the directory exists (b) you have permissions to read from the directory (c) the file exists (d) that you have permissions to read the file (try opening it through the OS) and (e) that the oracle user has permissions to open the file (run the GRANT command). I would suggest searching online for ORA-29283 and going through things people suggest for fixing the permissions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.