I want to generate a whole lot of SQL*Plus scripts by querying the data dictionary, but I’m hitting some problems and suspect I’m missing something obvious.
For example, when I execute the following in SQL*Plus I get ORA-01756: quoted string not properly terminated:
SQL> SPOOL myscript.sql
SQL> SELECT q'[SPOOL log
2 SELECT COUNT(*) FROM DUAL;
ERROR:
ORA-01756: quoted string not properly terminated
I tried using the line continuation character to avoid this error, but it puts the continuation character into the output:
SQL> SELECT q'[SPOOL log
2 SELECT COUNT(*) FROM DUAL; -
3 PROMPT Done.
4 ]' FROM DUAL;
SPOOL log
SELECT COUNT(*) FROM DUAL; -
PROMPT Done.
Notice how the output has the - after DUAL;? I don’t want that in the generated script.
One way I can get around this is to concatenate a lot of CHR() function calls to generate semicolons and linefeeds; but I hope I don’t have to because these scripts being generated are very long, and having bits like ]'||CHR(59)||CHR(10)||q'[ scattered throughout the code makes it look very ugly and a pain to troubleshoot.
(I’m using SQL*Plus Release 11.2.0.1.0 Production, connecting to an 11gR2 instance.)
The problem is that SQL*Plus is interpreting your first
;as the terminator for the command. You may have noticed that if you write your commands to a text file and execute that (or edit it in a text editor from with SQL*Plus) it works.To make it work with live typing, if you really want to do that (seems unlikely if they’re going to be very long!), you can turn off the automatic detection of the terminator with
SET SQLTERMINATOR off. Note that you’ll have to tell SQL*Plus that you’re done and that it should execute with the/instruction as the second;is ignored as well.If you’re building these from the data dictionary, another option is to use PL/SQL to do the queries and manipulations and
dbms_outputto produce the output you’re gong to spool, as long as the final file size won’t exceed the buffer limits.