How can I remove the string "connection established" in an SQL script which is connection to different databases and spools some output in one global file?
This is my simple script actually:
set feedback off
set echo off
set lines 200
set pages 999
spool check_control_file_record_keep_time.txt;
conn /#"HOST1:1521/DB1"
set echo off
set feedback off
set serveroutput on
DECLARE
actual_param_setting NUMBER;
BEGIN
select value into actual_param_setting from v$parameter where name='control_file_record_keep_time';
IF actual_param_setting >= 11 THEN
DBMS_OUTPUT.PUT_LINE('[OK] - DB1 - Parameter control_file_record_keep_time for this database is OK!');
ELSE
DBMS_OUTPUT.PUT_LINE('[NOK] - DB1 - Parameter control_file_record_keep_time need to be changed for this database!');
END IF;
END;
/
conn /#"HOST2:1521/DB2"
set echo off
set feedback off
set serveroutput on
DECLARE
actual_param_setting NUMBER;
BEGIN
select value into actual_param_setting from v$parameter where name='control_file_record_keep_time';
IF actual_param_setting >= 11 THEN
DBMS_OUTPUT.PUT_LINE('[OK] - DB2 - Parameter control_file_record_keep_time for this database is OK!');
ELSE
DBMS_OUTPUT.PUT_LINE('[NOK] - DB2 - Parameter control_file_record_keep_time need to be changed for this database!');
END IF;
END;
/
conn /#"HOST3:1521/DB3"
set echo off
set feedback off
set serveroutput on
DECLARE
actual_param_setting NUMBER;
BEGIN
select value into actual_param_setting from v$parameter where name='control_file_record_keep_time';
IF actual_param_setting >= 11 THEN
DBMS_OUTPUT.PUT_LINE('[OK] - DB3 - Parameter control_file_record_keep_time for this database is OK!');
ELSE
DBMS_OUTPUT.PUT_LINE('[NOK] - DB3 - Parameter control_file_record_keep_time need to be changed for this database!');
END IF;
END;
/
spool off;
exit
And this is the output in spooled file
Connection established.
[NOK] - DB1 - Parameter control_file_record_keep_time need to be changed for this database!
Connection established.
[NOK] - DB2 - Parameter control_file_record_keep_time need to be changed for this database!
Connection established.
[NOK] - DB3 - Parameter control_file_record_keep_time need to be changed for this database!
Any idea to "throw away" the "Connection established." by executing in sqlplus?
You can use the -s[ilent] flag when you invoke SQL*Plus; from the documentation:
3.5.1.10 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.
That applies to connect commands issued within the program too, not just the initial connection (if you have one; you may be using /nolog).
You could also then redirect the output to a file as an alternative to using spool - by default the banner etc. would be in the output, but as this suppresses that, the output is much cleaner.
Related
I'm developing a code and would like to see in the results only the actual values, instead of the script I executed.
So I'm trying to develop a "table rows counter" and I've been quite successful, but I still get the full executed script, when I only need its results.
Let's say, my code is:
DECLARE
VAR_1 NUMBER := 0;
BEGIN
SELECT IMITM INTO VAR_1 FROM PRODDTA.F4101 WHERE ROWNUM <= 1;
DBMS_OUTPUT.PUT_LINE('DUMMY TEST 1' || VAR_1);
DBMS_OUTPUT.PUT_LINE('DUMMY TEST 2');
END;
/
Well, by clicking F5 I would see:
DUMMY TEST 150041087
DUMMY TEST 2
PL/SQL procedure successfully completed.
In the complete code it reads about 200 lines of only code, so...
How can I get only the output lines, and in this case, remove the "PL/SQL procedure..." legend?
I've tried unsuccessfully yo use "SET FEED" and "SET TERM" off.
According to Oracle documentation:
SET FEEDBACK OFF also turns off the statement confirmation messages
such as 'Table created' and 'PL/SQL procedure successfully completed'
that is displayed after successful SQL or PL/SQL statements.
You need to use SET FEEDBACK OFF as following:
SQL> SET SERVEROUT ON
SQL> SET FEEDBACK OFF
SQL> DECLARE
2 VAR_1 NUMBER := 0;
3 BEGIN
4 SELECT
5 1
6 INTO VAR_1
7 FROM
8 DUAL
9 WHERE
10 ROWNUM <= 1;
11
12 DBMS_OUTPUT.PUT_LINE('DUMMY TEST 1' || VAR_1);
13 DBMS_OUTPUT.PUT_LINE('DUMMY TEST 2');
14 END;
15 /
DUMMY TEST 11
DUMMY TEST 2
SQL>
SQL>
Cheers!!
I actually managed to do it by exploring SET commands.
For the SQL script "acknolwedgement", we can skip it with the following commands:
To disable the SQL & variables verification before the actual execution, use:
SET VERIFY OFF
To disable the post execution notes, as Tejash indicated already, use:
SET FEEDBACK OFF
So my prologue ends up being:
SET SERVERTOUTPUT ON;
SET VERIFY OFF;
SET FEEDBACK OFF;
Thanks a lot for the answers & comments.
I have written a program to write a file into a directory using utl_file.
sample script :
PROCEDURE XX_FILE_TRANS (X_RETCODE VARCHAR2,X_ERRBUF VARCHAR2)
IS
CURSOR ORG_CUR IS
SELECT
'"'||XTRO.IDENTIFIER||'","'||
XTRO.ORGANIZATION_CODE||'","'||
XTRO.NAME ||'","'||
XTRO.PARENT_CODE ||'","'||
XTRO.INDUSTARY_NUMBER ||'","'||
XTRO.STATUS||'","'||
XTRO.SEQUENCE||'"' "ORG_DATA"
FROM abc XTRO;
ORG_REC ORG_CUR%rowtype;
begin
begin
delete XXHCM.XXHR_TAL_REC_ORGANIZATION_hist
where 1 = 1
AND creation_date < TRUNC (SYSDATE - 60);
COMMIT;
EXCEPTION
when OTHERS
then
null;
end;
LOAD_archive_TABLE; -- call the history table procedure
DBMS_OUTPUT.PUT_LINE('start');
fileHandler := UTL_FILE.FOPEN('INTF_DIR_INBOUND', LC_OLF_ORG, 'W');
UTL_FILE.put_line(fileHandler, 'Identifier,OrgCode,OrgName,ParentCode,IndustryNumber,StatusDescription,Sequence');
FOR ORG_REC IN ORG_CUR
LOOP
UTL_FILE.put_line(fileHandler,ORG_REC.ORG_DATA);
END LOOP;
UTL_FILE.FCLOSE(fileHandler);
dbms_output.put_line('end');
EXCEPTION
WHEN UTL_FILE.INVALID_PATH THEN
FND_FILE.PUT_LINE(FND_FILE.LOG,' Invalid File Path');
UTL_FILE.FCLOSE(fileHandler);
dbms_output.put_line('1'||sqlerrm);
WHEN UTL_FILE.WRITE_ERROR THEN
FND_FILE.PUT_LINE(FND_FILE.LOG,' Write Permission on does not exist');
UTL_FILE.FCLOSE(fileHandler);
dbms_output.put_line('2'||sqlerrm);
WHEN UTL_FILE.INVALID_MODE THEN
FND_FILE.PUT(FND_FILE.LOG,'THE INVALID MODE OF DATA FILE');
UTL_FILE.FCLOSE(fileHandler);
dbms_output.put_line('3'||sqlerrm);
WHEN UTL_FILE.INVALID_OPERATION THEN
FND_FILE.PUT(FND_FILE.LOG,' THE FILE CANNOT BE OPENED AS REQUESTED.');
UTL_FILE.FCLOSE(fileHandler);
dbms_output.put_line('4'||sqlerrm);
WHEN UTL_FILE.INVALID_MAXLINESIZE THEN
FND_FILE.PUT(FND_FILE.LOG,'THE SPECIFIED MAXIMUM LINE SIZE IS TOO LARGE OR TOO SMALL.');
UTL_FILE.FCLOSE(fileHandler);
dbms_output.put_line('5'||sqlerrm);
WHEN UTL_FILE.ACCESS_DENIED THEN
FND_FILE.PUT(FND_FILE.LOG,'ACCESS TO THE DIRECTORY OBJECT IS DENIED.');
FND_FILE.PUT(FND_FILE.LOG,'OPERATING SYSTEM ERROR OCCURED DURING THE WRITE OPERATION.');
UTL_FILE.FCLOSE(fileHandler);
dbms_output.put_line('6'||sqlerrm);
WHEN UTL_FILE.CHARSETMISMATCH THEN
FND_FILE.PUT(FND_FILE.OUTPUT,'THIS FILE IS OPEN FOR NCHAR DATA.');
UTL_FILE.FCLOSE(fileHandler);
dbms_output.put_line('7'||sqlerrm);
END;
Now the server i have is not db server because of whch i cannot use UTL_FILE.
Is thee any another way than UTL_FILE to write into a file ?
You can create an EXTERNAL TABLE instead...
https://docs.oracle.com/cd/B19306_01/server.102/b14215/et_concepts.htm
Why not use sqlplus? Create a simple .sql file (my_script.sql) containing your SQL, with a few SET commands, something like:
SET FEEDBACK OFF;
SET ECHO OFF;
SET TERM OFF;
SET serveroutput on size unlimited;
SET linesize 30000
SET pagesize 0
SET head off
SET trims on
SET verify off
WHENEVER SQLERROR EXIT SQL.SQLCODE
SPOOL my_file.csv;
select
'MY_NUMBER_FIELD1,'||
'MY_VARCHAR_FIELD2,'||
'MY_VARCHAR_FIELD3'
from dual
UNION ALL
select
COL1||','||
'"'||replace(COL2, '"','""')||'",'||
'"'||replace(COL3, '"','""')||'"'
from MY_TABLE
/
Modify the SQL as needed. Place that sql file in a directory on a server of your choice, login to Oracle from that directory using sqlplus, and run:
#my_script.sql
The output file (my_file.csv) will be in the directory you logged in from. A sqlplus login script can be used to automate things as well (along with cron or task scheduler or other scheduling software).
I am trying to display the result of an update statement "5 rows inserted" on my shell script.
One way i could figure out is
Declare count number;
Begin
Update tablename set colname="k";
count=sql%rowcount;
Dbms.out.put_line(count);
Commit;
End
The sql plus count variable is getting the value but i am unable to use it in my shell script.
I have to copy the value of sql plus variable into a shell script variable .but dont know how to do it.
Please help , also let me know if there is any other way to do it .
Thanks in advance
Have SQL*Plus exit with with the value you want. Here's an example (run this as a UNIX script):
sqlplus my_username/my_password#my_database << EOF
variable count number;
begin
update mtl_system_items -- your update goes here!
set last_update_date = last_update_date
where rownum <= 5;
:count := SQL%ROWCOUNT;
DBMS_OUTPUT.PUT_LINE(:count);
ROLLBACK;
END;
/
EXIT :COUNT;
EOF
echo Return code was $?
That is, when you EXIT :COUNT from SQL*Plus, the value of your COUNT variable will be accessible in $? in your UNIX script. You'll need to save it off immediately into another variable, since $? will get overwritten if you run any other commands in your script.
I suggest writing a variable assignment in a spool file and then reading that back in your shell script. For example:
sqlplus -s apps/apps#vis <<EOF
set feedback off lines 150 pages 0 head off termout off serveroutput on size 10000 echo off
variable count number;
begin
-- Emulation of update that updates 40000 rows
:count := 40000;
end;
/
spool outvars.sh
begin
dbms_output.put_line('COUNT=' || :count);
end;
/
spool off
exit
EOF
. outvars.sh
echo "Back in shell, COUNT=$COUNT"
will result in this output:
COUNT=40000
Back in shell, COUNT=40000
Although slightly more indirect it has the advantage of being able to pass multiple values with a pretty arbitrary content, not just a single number limited to a small integer range.
I am stuck with this code:
REM AWR-Generator.sql: Script for creating multiple consecutive Oracle AWR Reports
REM
REM Creates an output SQL script which, when run, will generate all AWR Reports
REM between the specificed start and end snapshot IDs, for all instances
REM
REM For educational purposes only - no warranty is provided
REM Test thoroughly - use at your own risk
REM
set feedback off
set echo off
set verify off
set timing off
-- Set AWR_FORMAT to "text" or "html"
define AWR_FORMAT = 'text'
define DEFAULT_OUTPUT_FILENAME = 'awr-generate.sql'
define NO_ADDM = 0
-- Get values for dbid and inst_num before calling awrinput.sql
set echo off heading on
column inst_num heading "Inst Num" new_value inst_num format 99999;
column inst_name heading "Instance" new_value inst_name format a12;
column db_name heading "DB Name" new_value db_name format a12;
column dbid heading "DB Id" new_value dbid format 9999999999 just c;
prompt
prompt Current Instance
prompt ~~~~~~~~~~~~~~~~
select d.dbid dbid
, d.name db_name
, i.instance_number inst_num
, i.instance_name inst_name
from v$database d,
v$instance i;
-- Call the Oracle common input script to setup start and end snap ids
##?/rdbms/admin/awrinput.sql
-- Ask the user for the name of the output script
prompt
prompt Specify output script name
prompt ~~~~~~~~~~~~~~~~~~~~~~~~~~
prompt This script produces output in the form of another SQL script
prompt The output script contains the commands to generate the AWR Reports
prompt
prompt The default output file name is &DEFAULT_OUTPUT_FILENAME
prompt To accept this name, press <return> to continue, otherwise enter an alternative
prompt
set heading off
column outfile_name new_value outfile_name noprint;
select 'Using the output file name ' || nvl('&&outfile_name','&DEFAULT_OUTPUT_FILENAME')
, nvl('&&outfile_name','&DEFAULT_OUTPUT_FILENAME') outfile_name
from sys.dual;
set linesize 800
set serverout on
set termout off
-- spool to outputfile
spool &outfile_name
-- write script header comments
prompt REM Temporary script created by awr-generator.sql
prompt REM Used to create multiple AWR reports between two snapshots
select 'REM Created by user '||user||' on '||sys_context('userenv', 'host')||' at '||to_char(sysdate, 'DD-MON-YYYY HH24:MI') from dual;
set heading on
-- Begin iterating through snapshots and generating reports
DECLARE
c_dbid CONSTANT NUMBER := :dbid;
c_inst_num CONSTANT NUMBER := :inst_num;
c_start_snap_id CONSTANT NUMBER := :bid;
c_end_snap_id CONSTANT NUMBER := :eid;
c_awr_options CONSTANT NUMBER := &&NO_ADDM;
c_report_type CONSTANT CHAR(4):= '&&AWR_FORMAT';
v_awr_reportname VARCHAR2(100);
v_report_suffix CHAR(5);
CURSOR c_snapshots IS
select inst_num, start_snap_id, end_snap_id
from (
select s.instance_number as inst_num,
s.snap_id as start_snap_id,
lead(s.snap_id,1,null) over (partition by s.instance_number order by s.snap_id) as end_snap_id
from dba_hist_snapshot s
where s.dbid = c_dbid
and s.snap_id >= c_start_snap_id
and s.snap_id <= c_end_snap_id
)
where end_snap_id is not null
order by inst_num, start_snap_id;
BEGIN
dbms_output.put_line('');
dbms_output.put_line('prompt Beginning AWR Generation...');
dbms_output.put_line('set heading off feedback off lines 800 pages 5000 trimspool on trimout on');
-- Determine report type (html or text)
IF c_report_type = 'html' THEN
v_report_suffix := '.html';
ELSE
v_report_suffix := '.txt';
END IF;
-- Iterate through snapshots
FOR cr_snapshot in c_snapshots
LOOP
-- Construct filename for AWR report
v_awr_reportname := 'awrrpt_'||cr_snapshot.inst_num||'_'||cr_snapshot.start_snap_id||'_'||cr_snapshot.end_snap_id||v_report_suffix;
dbms_output.put_line('prompt Creating AWR Report '||v_awr_reportname
||' for instance number '||cr_snapshot.inst_num||' snapshots '||cr_snapshot.start_snap_id||' to '||cr_snapshot.end_snap_id);
dbms_output.put_line('prompt');
-- Disable terminal output to stop AWR text appearing on screen
dbms_output.put_line('set termout off');
-- Set spool to create AWR report file
dbms_output.put_line('spool '||v_awr_reportname);
-- call the table function to generate the report
IF c_report_type = 'html' THEN
dbms_output.put_line('select output from table(dbms_workload_repository.awr_report_html('
||c_dbid||','||cr_snapshot.inst_num||','||cr_snapshot.start_snap_id||','||cr_snapshot.end_snap_id||','||c_awr_options||'));');
ELSE
dbms_output.put_line('select output from table(dbms_workload_repository.awr_report_text('
||c_dbid||','||cr_snapshot.inst_num||','||cr_snapshot.start_snap_id||','||cr_snapshot.end_snap_id||','||c_awr_options||'));');
END IF;
dbms_output.put_line('spool off');
-- Enable terminal output having finished generating AWR report
dbms_output.put_line('set termout on');
END LOOP;
dbms_output.put_line('set heading on feedback 6 lines 100 pages 45');
dbms_output.put_line('prompt AWR Generation Complete');
-- EXCEPTION HANDLER?
END;
/
spool off
set termout on
prompt
prompt Script written to &outfile_name - check and run in order to generate AWR reports...
prompt
--clear columns sql
undefine outfile_name
undefine AWR_FORMAT
undefine DEFAULT_OUTPUT_FILENAME
undefine NO_ADDM
undefine OUTFILE_NAME
set feedback 6 verify on lines 100 pages 45
This piece of code generates a spool file with consecutive snap ids between start snap and end snap. I want to modify it, such that it asks for an interval value and calculate start_snap and end_snap with that interval.
For eg.
If I give start_snap as 1400 and end_snap as 1404 with interval as 2, then it should give reports with 1400 to 1402, 1403 to 1404.
Regards,
Kaushal.
I am using Oracle SQL (in SQLDeveloper, using the SQL Worksheet). I would like to print a statement before my select, such as
PRINT 'Querying Table1';
SELECT * from Table1;
What do I use to Print / show text output? It's not Print, because that gives me the error: Bind Variable Table1 is NOT DECLARED. DBMS_OUTPUT.PUT_LINE is an unknown command. (Obviously, I'm an inexperienced SQLDeveloper and Oracle user. There must be some synonym for Print, but I'm having trouble finding help on it without knowing what it is.)
for simple comments:
set serveroutput on format wrapped;
begin
DBMS_OUTPUT.put_line('simple comment');
end;
/
-- do something
begin
DBMS_OUTPUT.put_line('second simple comment');
end;
/
you should get:
anonymous block completed
simple comment
anonymous block completed
second simple comment
if you want to print out the results of variables, here's another example:
set serveroutput on format wrapped;
declare
a_comment VARCHAR2(200) :='first comment';
begin
DBMS_OUTPUT.put_line(a_comment);
end;
/
-- do something
declare
a_comment VARCHAR2(200) :='comment';
begin
DBMS_OUTPUT.put_line(a_comment || 2);
end;
your output should be:
anonymous block completed
first comment
anonymous block completed
comment2
PROMPT text to print
Note: must use
Run as Script (F5)
not
Run Statement (Ctl + Enter)
The main answer left out a step for new installs where one has to open up the dbms output window.
Then the script I used:
dbms_output.put_line('Start');
Another script:
set serveroutput on format wrapped;
begin
DBMS_OUTPUT.put_line('jabberwocky');
end;
You could set echo to on:
set echo on
REM Querying table
select * from dual;
In SQLDeveloper, hit F5 to run as a script.
You could put your text in a select statement such as...
SELECT 'Querying Table1' FROM dual;
For me, I could only get it to work with
set serveroutput on format word_wrapped;
The wraped and WRAPPED just threw errors: SQLPLUS command failed - not enough arguments
If I ommit begin - end it is error. So for me this is working (nothing else needed):
set serveroutput on;
begin
DBMS_OUTPUT.PUT_LINE('testing');
end;
If you don't want all of your SQL statements to be echoed, but you only want to see the easily identifiable results of your script, do it this way:
set echo on
REM MyFirstTable
set echo off
delete from MyFirstTable;
set echo on
REM MySecondTable
set echo off
delete from MySecondTable;
The output from the above example will look something like this:
-REM MyFirstTable
13 rows deleted.
-REM MySecondTable
27 rows deleted.