Some scripts I have inherited will blindly call SET FEEDBACK OFF or SET ECHO OFF at the beginning of the script, then set them to ON or OFF at the end of the script. I would like to modify these scripts to determine what value was set before the script was run, and set the environment back to that value when the script completes.
How do I query SQL Plus environment values, store them, and restore them when a script has finished?
One method I have thought of:
SPOOL env-backup.sql
SHOW ECHO FEEDBACK TIMING
REM ...
#env-backup.sql
But
The values SHOW ECHO FEEDBACK TIMING spits out can't be executed directly (ECHO OFF vs SET ECHO OFF)
I would rather not create yet another file (or any modifications to the DB)
Not that it is necessarily related, but I'm using SqlPlus from Oracle XE (10g) on Windows
SQL*Plus has the STORE command just for this. It outputs a file which has all the environment settings. Executing the file would restore these settings. Type `HELP STORE' from the SQL*Plus prompt for more info.
You might be interested in those scripts
If you want each SQL to be run independently you could call them using the HOST command. That is, for misbehaving scripts call HOST SQLPLUS username/password#tnsname #script.sql and it will run in a new process.
Why not just put the desired values for your connection in the glogin.sql script provided?
It's normally found in %ORACLE_HOME%\sqlplus\admin
From my glogin.sql:
--
-- Copyright (c) 1988, 2005, Oracle. All Rights Reserved.
--
-- NAME
-- glogin.sql
--
-- DESCRIPTION
-- SQL*Plus global login "site profile" file
--
-- Add any SQL*Plus commands here that are to be executed when a
-- user starts SQL*Plus, or uses the SQL*Plus CONNECT command.
--
-- USAGE
-- This script is automatically run
--
set pagesize 60
set linesize 500
set wrap off
Then just reconnnect after running any scripts that alter your environment (or run #glogin.sql itself).
Related
I have a stored procedure in an Oracle database and I want to create a batch file that can call and run the said procedure. After running the stored procedure I want to call a certain sql file then run it also within just a single batch file.
name of stored procedure = health_check
name of SQL file = spool1.sql
I want to run the stored procedure first in the batch file. Upon success I want to call the said sql file then run it still at the same batch file.
I'm already able to execute the sql file. I just want to add the calling and running of the stored procedure in my current batch file
I want to achieve something like this:
#echo off
--run proc here
sqlplus user/password#DB #D:\mysqlfile.sql
Is this possible? Thanks in advance.
You could use a 'heredec' approach:
#echo off
#(
echo execute health_check
echo #D:\mysqlfile.sql
) | sqlplus -s -l user/password#DB
Untested but this pattern should work. Everythign in the parentheses is evaluated, which produces two lines of output:
execute health_check
#D:\mysqlfile.sql
and those are treated as input by SQL*Plus. So it's the equivalent of an interactive session where you start SQL*Plus and then enter those two lines in turn at the SQL> prompt. The #<file> can be used from that prompt too to run the file contents. And execute (or just exec) is a client shortcut for an anonymous PL/SQL block.
This assumes, based on how you did this in your question, that the .sql file ends with an exit. If it doesn't then you can add then to the heredoc with echo exit.
I'm trying to run the following SQLPlus* set of commands:
/usr/tmp/> sqlplus -s / #my_test_script param1 param2 <<-EOF
SET ECHO OFF
SET HEADING OFF
SET VERIFY OFF
SET TERMOUT OFF
SET FEEDBACK OFF
SET PAGES 0
SET LINESIZE 400
EXIT
EOF
The SET commands are supposed to suppress the output from the script I'm running - but they have no effect, since the script itself probably has a different "scope" for ECHO, HEADING, etc.
The output is suppressed only once I move all those SET commands into the script-file itself.
Since this piece of code should eventually turn into a generic script for running other SQL scripts, putting those SET command inside every script is not a good solution.
Does anyone know of a way to force the SET command values over scripts ran within the same SQLPlus* "session"?
I agree with Shannon. You could try the following code (which changes the ordering a little bit):
/usr/tmp/> sqlplus -s / <<-EOF
SET ECHO OFF
SET HEADING OFF
SET VERIFY OFF
SET TERMOUT OFF
SET FEEDBACK OFF
SET PAGES 0
SET LINESIZE 400
#my_test_script param1 param2
EXIT
EOF
if I remember well, you could call from every script this initializing script using ## command. for example:
##init.sql
source
EDIT
also, from the oracle documentation :
SQL*Plus also supports a User Profile, executed after the Site Profile. This file is generally named login.sql. SQL*Plus searches for the user profile in your current directory, and then the directories you specify with the SQLPATH environment variable. SQL*Plus searches this colon-separated list of directories in the order they are listed.
You can add any SQL commands, PL/SQL blocks, or SQL*Plus commands to your user profile. When you start SQL*Plus, it automatically searches for your user profile and runs the commands it contains.
Is there a way to execute the start command with a variable as name. Actually, I need to provide a path that changes, but the filename is still the same, thus I decided to put a value in a user variable via Define or Accept
However, I receive error when I issue:
define name /home/andres/myfile.sql
start &name
-- Same error
start &name.
-- With an env variable, the same problem appears. (defining name in the shell)
start $name
start %name%
How can I execute a script with a dynamic name directly from SQLPlus? I know that I can do that from shell, but that will be platform dependent.
Finally, I am using CLPPlus, and that should have the same behavior as SQLPlus.
It seems to work for me on Unix. Next time please provide the error.
Your code errors : SP2-0136: DEFINE requires an equal sign (=)
Define needs an = sign.
dual.sql
select * from dual;
start.sql
start &1
define filename=/home/oracle/dual.sql
start &filename
Run script
SQL> #start dual
D
-
X
D
-
X
Using sqlplus.exe I'm looking for a way to write sqlplus output to a file.
Is there anyway I can do that, currently the output is written only to the console.
You may use the SPOOL command to write the information to a file.
Before executing any command type the following:
SPOOL <output file path>
All commands output following will be written to the output file.
To stop command output writing type
SPOOL OFF
Also note that the SPOOL output is driven by a few SQLPlus settings:
SET LINESIZE nn - maximum line width; if the output is longer it will wrap to display the contents of each result row.
SET TRIMSPOOL OFF|ON - if set OFF (the default), every output line will be padded to LINESIZE. If set ON, every output line will be trimmed.
SET PAGESIZE nn - number of lines to output for each repetition of the header. If set to zero, no header is output; just the detail.
Those are the biggies, but there are some others to consider if you just want the output without all the SQLPlus chatter.
Make sure you have the access to the directory you are trying to spool. I tried to spool to root and it did not created the file (e.g c:\test.txt). You can check where you are spooling by issuing spool command.
just to save my own deductions from all this is (for saving DBMS_OUTPUT output on the client, using sqlplus):
no matter if i use Toad/with polling or sqlplus, for a long running script with occasional dbms_output.put_line commands, i will get the output in the end of the script execution
set serveroutput on; and dbms_output.enable(); should be present in the script
to save the output SPOOL command was not enough to get the DBMS_OUTPUT lines printed to a file - had to use the usual > windows CMD redirection. the passwords etc. can be given to the empty prompt, after invoking sqlplus. also the "/" directives and the "exit;" command should be put either inside the script, or given interactively as the password above (unless it is specified during the invocation of sqlplus)
In windows type this command:
sqlplus.exe username/password#servicename #yourquery.sql > out.txt
besides, you should write an exit command at the end of "yourquery.sql" file.
In SQL PLus, if I want to hide the command on screen, I use at the beginning of the script:
SET ECHO OFF
But what about this command? How to hide also this?
In MS-Dos you could use # in front of it, but in SQL*Plus?
EDIT:
I know the -S option for SQL*Plus, but the problem is that I am sending my script to the client and therefore I don't control how they are running SQL*Plus.
You might want to start SQL*Plus with the -S flag:
sqlplus -S user/password#db #path/to/your/script
The documentation says: SILENT Option
-S[ILENT]
Suppresses all SQL*Plus information and prompt messages, including the command prompt, the echoing of commands, and the banner normally displayed when you start SQL*Plus. If you omit username or password, SQL*Plus prompts for them, but the prompts are not visible! Use SILENT to invoke SQL*Plus within another program so that the use of SQL*Plus is invisible to the user.
SILENT is a useful mode for creating reports for the web using the SQLPLUS -MARKUP command inside a CGI script or operating system script. The SQL*Plus banner and prompts are suppressed and do not appear in reports created using the SILENT option.SILENT Option
So, with the silent option it seems that there won't be need to use set echo at all.
No, I know of no way to hide the 'set echo off' command itself, if echo is on.
However, echo is off by default. So, if echo is on when you initially start SQL*Plus, perhaps you have a login.sql or glogin.sql that contains 'set echo on'?
Check for glogin.sql in $ORACLE_HOME/sqlplus/admin, and for login.sql in the directory where you started SQL*Plus and any directory listed in $SQLPATH.
Hope that helps.