Skip to Main Content
  • Questions
  • SQL Plus doesn't consider special characters in filenames

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Alexander.

Asked: December 20, 2018 - 3:21 pm UTC

Last updated: May 17, 2021 - 2:22 pm UTC

Version: SQL*Plus: Release 12.2.0.1.0 Production

Viewed 10K+ times! This question is

You Asked

There are files that contain special characters (i.e. $, #) in the name, and SQL Plus cannot find them.

$ sqlplus -L user/password@host:port/SID @/path/to/file/create$spec_view.sql

SQL*Plus: Release 12.2.0.1.0 Production on Thu Dec 13 17:29:54 2018

Copyright (c) 1982, 2016, Oracle. All rights reserved.

Last Successful login time: Thu Dec 13 2018 17:29:16 +03:00

Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

SP2-0310: unable to open file "/path/to/file/create$spec_view.sql
SQL> Disconnected from Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production


with LiveSQL Test Case:

and Chris said...

Really this is a problem with how Linux processes files. The dollar character denotes a variable. Which causes problems when passing the file to programs. You'll find you can read it fine in Windows (before 19.3; from this release you can no longer read Windows files with $ in their name).

So the real solution here is to stick to alphanumeric characters, period, underscore, and hyphens (though not at the start!) for your filenames. You're likely to get issues with other programs if you use other special characters.

If you absolutely must have a dollar, you can use the escchar variable, as described in MOS note 761384.1:

SQL*Plus 10g rel2 or 11g in Unix/Linux displays an error trying to create or read a file that includes a special character like the dollar sign in the name, ie, test$.lst

-- Steps To Reproduce:

SQL> spool test$.lst
SP2-0332: Cannot create spool file.

Or

SQL>start /tmp/test$.lst
SP2-0310: unable to open file "/tmp/test$.lst"

-- Business Impact:

Scripting the extraction of DDL files from the database, but some user objects have "$" in the name and it causes the SPOOL command to fail. In other cases, it is an application requirement having files including a $ symbol.

-- What is working:

Scripts work on MS Windows platform.
Cause

1) BUG:11985886 'SP2-310 unable to open file error when file name ending with "$"' explains when the I/O calls were changed in later versions to use RDBMS Core Functions instead of OS calls, it impacted the way the special characters were handled.

2) This issue is already fixed in Patch Set 10.2.0.4. A new ESCCHAR variable is included there and in SQL*Plus 11g:

http://download.oracle.com/docs/cd/B28359_01/server.111/b31189/ch12040.htm#sthref2074
SQL*PlusĀ® User's Guide and Reference
Release 11.1

SET ESCCHAR {@ | ? | % | $ | OFF}

Specifies a character to be escaped and not interpreted when used in a file name for the SPOOL, START, @, RUN and EDIT commands.
Solution

1) If you are using SQL*Plus 10gR2, apply Patch Set 10.2.0.4, where this bug is fixed.

a) Go to My Oracle Support (Metalink) -> Patches -> Simple Search -> Patch Number: 6810189

Review the readme and test it before applying on production environment. It is the database patchset, but can be applied on the Database Client if just SQL*Plus is installed in your ORACLE_HOME.

b) The new variable should be recognized in 10.2.0.4. Test as follows:

SQL> SET ESCCHAR $
SQL> spool /tmp/test$.lst

2) If you are in SQL*Plus 11g release, the variable ESCCHAR to escape the especial characters will be recognized without patches. When user switch on the ESCCHAR, then that character will not be translated in the filename:

SET ESCCHAR [@|?|$|OFF] - The default is OFF

Examples

a) Spooling:

SQL>SET ESCCHAR $
SQL> spool /tmp/test$.lst
SQL> spool off

b) Reading a file:

SQL>SET ESCCHAR $
SQL> start /tmp/myquery$.lst

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

ESCCHAR not works on SQLcl

Alexander, December 09, 2020 - 3:19 pm UTC

Hello, Tom!
Unfortunately tis doesnot works on SQLcl, I have the following output:
"SET ESCCHAR $" is Obsolete.

And later:
Error starting at line : 1 in command:
@/builds/gdwh/deploy/patches/../ora/views/cat_map.dm_cnp_to_acnt$ds.sql
Error:
SP2-0310: Unable to open file: "/builds/gdwh/deploy/patches/../ora/views/cat_map.dm_cnp_to_acnt.sql

As you can see "$" was eaten in the last line
How could this be solved?
Thank you
Connor McDonald
December 11, 2020 - 3:40 am UTC

Are you running that just from inside sqlcl ? I can't reproduce.

SQL> version
Oracle SQLDeveloper Command-Line (SQLcl) version: 20.2.0.0 build: 20.2.0.174.1557
SQL> host ls -l /tmp/*.sql
-rw-r--r--. 1 oracle oinstall 20 Dec 11 11:37 /tmp/x$.sql

SQL> host cat /tmp/*.sql
select * from dual;

SQL> @/tmp/x$.sql

   DUMMY 
________ 
X        





Does not hold for Oracle 19c or newer

Martin Kubis, May 17, 2021 - 12:45 pm UTC

In case that you will stumble upon this topic, I found out that dollar signs are no longer supported within the file names on Windows:

Note:
Starting from Oracle Database release 19c, version 19.3, file names with the $ character will no longer run on Windows.

Source: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqpug/SET-system-variable-summary.html#GUID-37A75C28-8B05-4D5E-838C-08328D51FBB6

Chris Saxon
May 17, 2021 - 2:22 pm UTC

Thanks, I've added a note about this in the main question