Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to echo text during SQL script execution in SQLPLUS

I have a batch file which runs a SQL script in sqlplus and sends the output to a log file:

sqlplus user/pw < RowCount.sql > RowCount.log

My log file contains this:

Connected to: Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production  SQL> SQL>    COUNT(*) ----------      0  SQL>    COUNT(*) ----------      0 

etc. but it's several thousand lines of output and therefore hard to determine which results belong to which statement.

I would like to add some formatting to the output, so that I may discern what happened. Either an echo of the executed statement or manually inserting some "echo" statements into the script would be fine. Ideally it would look something like this:

SQL> select(*) from TableA;   COUNT(*) ----------      0  SQL> select(*) from TableB;   COUNT(*) ----------      0 
like image 598
glenneroo Avatar asked Nov 07 '13 18:11

glenneroo


People also ask

Can you echo in SQL?

The ECHO setting tells SQL*Plus whether you want the contents of script files to be echoed to the screen as they are executed.

What are set echo in SQL?

SET ECHO {ON | OFF}Controls whether or not to echo commands in a script that is executed with @, @@ or START. ON displays the commands on screen. OFF suppresses the display. ECHO does not affect the display of commands you enter interactively or redirect to SQL*Plus from the operating system.

How do I comment in Sqlplus?

SQL*Plus does not interpret the comment as a command. The REMARK command must appear at the beginning of a line, and the comment ends at the end of the line. A line cannot contain both a comment and a command. A "–" at the end of a REMARK line is treated as a line continuation character.

How do I run a text file in Sqlplus?

Answer: To execute a script file in SQLPlus, type @ and then the file name. The above command assumes that the file is in the current directory. (ie: the current directory is usually the directory that you were located in before you launched SQLPlus.) This command would run a script file called script.


2 Answers

The prompt command will echo text to the output:

prompt A useful comment. select(*) from TableA; 

Will be displayed as:

SQL> A useful comment. SQL>    COUNT(*) ----------      0 
like image 135
glenneroo Avatar answered Sep 17 '22 15:09

glenneroo


You can use SET ECHO ON in the beginning of your script to achieve that, however, you have to specify your script using @ instead of < (also had to add EXIT at the end):

test.sql

SET ECHO ON  SELECT COUNT(1) FROM dual;  SELECT COUNT(1) FROM (SELECT 1 FROM dual UNION SELECT 2 FROM dual);  EXIT 

terminal

sqlplus hr/oracle@orcl @/tmp/test.sql > /tmp/test.log 

test.log

SQL>  SQL> SELECT COUNT(1) FROM dual;    COUNT(1) ----------      1  SQL>  SQL> SELECT COUNT(1) FROM (SELECT 1 FROM dual UNION SELECT 2 FROM dual);    COUNT(1) ----------      2  SQL>  SQL> EXIT
like image 23
Przemyslaw Kruglej Avatar answered Sep 18 '22 15:09

Przemyslaw Kruglej