Check if a file exists in UNIX from SQLplus without UTL_FILE


My current scenario is like this:

  • I need to login to sqlplus from a shell script to call a stored procedure.
  • After that I need to create a CSV file by SPOOLING data from a table.
  • Then I need to check whether the CSV file has been created in a particular directory and depending on the result an update query needs to be run.

I know that this can be checked within sqlplus with the help of UTL_FILE package but unfortunately due to Client policies,the access of this package is restricted in the current system.

Another way is to exit from sqlplus and perform the file check in UNIX and then again log in to sqlplus to perform the rest actions. But this I believe would result in slower execution time and performance is an important factor in this implementation as the tables contain huge volumes of data(in millions).

So is there any other way to check this from sqlplus without exiting from the current session?

System Info:

OS - Red Hat Enterprise Linux

Database - Oracle 11g

Show source
| database   | unix   | oracle11g   | sqlplus   2017-01-04 17:01 1 Answers

Answers to Check if a file exists in UNIX from SQLplus without UTL_FILE ( 1 )

  1. 2017-01-04 18:01

    If the file is on the same machine that you're running SQL*Plus on, you could potentially use the host command.

    If the file you're checking is the same one you're spooling to, it must exist anyway, or you would have got an SP error of some kind; but if you do want to check the same file for some reason, and assuming you have a substitution variable with the file name:

    define csv_file=/path/to/spool.csv
    -- call procedure
    spool &csv_file
    -- do query
    spool off
    host ls &csv_file
    update your_table
    set foo=bar
    where &_rc = 0;

    If the file exists when the host command is run, the _rc substitution variable will be set to zero. If the file doesn't exist or isn't readable for any reason it will be something else - e.g. 2 if the file just doesn't exist. Adding the check &_rc = 0 to your update will mean no rows are updated if there was an error. (You can of course still have whatever other conditions you need for the update).

    You could suppress the display of the file name by adding 1>/dev/null to the host command string; and could also suppress any error messages by also adding 2>/dev/null, though you might want to see those.

    The documentation warns against using &_rc as it isn't portable; but it works on RHEL so as long as you don't need your script to be portable to other operating systems this may be good enough for you. What you can't do, though, is do anything with the contents of the file, or interpret anything about it. All you have available is the return code from the command you run. If you need anything more sophisticated you could call a script that generates specific return codes, but that's getting a bit messy.

Leave a reply to - Check if a file exists in UNIX from SQLplus without UTL_FILE

◀ Go back