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.
Related
I'm trying to write a deployment script to run with SQL*Plus in a CI/CD pipeline but I can't find my way around what seems to be a very basic issue.
Here's a shortened version of the script release.sql:
DECLARE
vnum NUMBER;
BEGIN
SELECT COUNT(tname) INTO vnum FROM tab WHERE tname = 'DA_VERSION';
IF vnum = 0 THEN -- run create scripts
#ddl/da_001.sql
#ddl/da_002.sql
#dml/version.sql -- set initial version
END IF;
END;
da_001.sql looks like this:
CREATE TABLE TABLE_NAME
(
COLUMN1 NUMBER NOT NULL
, CONSTRAINT TABLE_NAME_PK PRIMARY KEY
(
COLUMN1
)
ENABLE
);
When I run
sqlplus.exe connection_string #release.sql
I get
CREATE TABLE DA_PRODUCTS
*
ERROR at line 6:
ORA-06550: line 6, column 1:
PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:...
So it doesn't like the CREATE statement at the beginning of da_001.sql but I don't know why. What am I missing here?
Create an installation file for your scripts, ie
install.sql
===========
#ddl/da_001.sql
#ddl/da_002.sql
#dml/version.sql -- set initial version
and then selectively call it via a wrapper in SQL Plus
set feedback off
set pages 0
spool /tmp/runme.sql
select
case when COUNT(tname) = 0 then '##install.sql' else 'pro Skipped install.sql' end
FROM tab WHERE tname = 'DA_VERSION';
spool off
#/tmp/runme.sql
As others have said there is a strict separation between what SQL*PLus understands/is capable of and what is within the SQL and PLSQL languages.
Script handling is one of these distinct areas, which means you cannot executing a SQL script within a PLSQL block.
Also, SQL*Plus does not have any understanding of PLSQL logic constructs.
However, looking at your requirement there might be a way. The answer from #"Connor McDonald" should work. Here is my attempt using a more PLSQL based approach.
This approach uses SQLPLus variables, which can be referenced and amended in both PLSQL and SQLPlus.
First you need a 'No Op' script since when using the SQL*PLus '#' you must specify a valid script name:
noop.sql:
PROMPT No Op
Now your controller script:
-- Declare your variables
VAR script1 VARCHAR2(256)
VAR script2 VARCHAR2(256)
VAR script3 VARCHAR2(256)
DECLARE
vnum NUMBER;
BEGIN
:script1 := 'noop.sql';
:script2 := 'noop.sql';
:script3 := 'noop.sql';
SELECT COUNT(tname) INTO vnum FROM tab WHERE tname = 'DA_VERSION';
IF vnum = 0 THEN -- run create scripts
-- Set variables
:script1 := 'ddl/da_001.sql';
:script2 := 'ddl/da_002.sql';
:script3 := 'dml/version.sql'; -- set initial version
END IF;
END;
/
-- Make variables referencable as SQLPLus defines
COLUMN s1 NEW_VALUE s1
COLUMN s2 NEW_VALUE s2
COLUMN s3 NEW_VALUE s3
SELECT :script1 s1, :script2 s2, :script3 s3
FROM dual;
-- RUN !!
# &&s1
# &&s2
# &&s3
The 3 scriptn variables can be used in PLSQL.
To use as SQL_PLus substition variable (&) we use the COL ... NEW_VALUE command to map a SELECT list column alias to a substituion variable. So we will effectively map scriptn to subs variabl;e sn.
After the PLSQL blocks completes the scriptn variables will have teh value 'noop.sql' or the name of teh script to run.
Then at the end, reference the subs variables in the '#' commands.
Anything with 'noop.sql' will execute a blank script.
I haven't used this myself but you might try something like this (simplified demo):
declare
vnum number := 0;
begin
--select count(tname) into vnum from tab where tname = 'DA_VERSION';
if vnum = 0 then -- include create script
execute immediate q'[
#callthis.sql
]';
end if;
end;
To work within execute immediate, the called script must contain a single statement and no semicolon.
I'm using PL/SQL Developer version 11.0.6.1776
When I launch the script in my command window, the name of the file I'm spooling is incomplete.
This is the script I have:
set pagesize 0
set long 90000
SET TERMOUT OFF
spool out.sql
select 'spool C:\Users\personal\MAIN_USR\index\'||REPLACE(index_name, '$', '_')||'.txt'||chr(13)||chr(10)||
'SELECT DBMS_METADATA.GET_DDL(''INDEX'','''||index_name||''',''MTO_INDX'') '||chr(13)||chr(10)||
'FROM DUAL;'||chr(13)||chr(10)||
'spool off' as cmd
FROM user_indexes ui
INNER JOIN MTTO_TAB_EXIST_ALL tea USING(table_name)
WHERE tea.MRK_DEL_PERM = 'Y'
AND tea.OWNER_NM = 'MTO_INDX'
AND MCA_BACKUP_DDL != 'Y';
spool off
#OUT.SQL
exit
In the spooling, some files look like this below:
spool C:\Users\personal\MAIN_USR\index\CIA_PRD_PRDR_DISTR_INV_3242.t
The file extension should be .txt, but for some reason it is incomplete and other spooling files only have a part of the name
I appreciate any help to fix this
Well, in SQL*Plus, it is e.g.
set linesize 100
which sets its length. I don't know whether it works in PL/SQL Developer as well.
On the other hand, even if line size is set to a low number (e.g. 50), SQL*Plus will "break" the line and move the rest of the text into the next line:
SQL> set linesize 50
SQL>
SQL> select 'spool C:\Users\personal\MAIN_USR\index\'||REPLACE(index_name, '$', '_')||'.txt'||chr(13)||chr(10)||
2 'SELECT DBMS_METADATA.GET_DDL(''INDEX'','''||index_name||''',''MTO_INDX'') '||chr(13)||chr(10)||
3 'FROM DUAL;'||chr(13)||chr(10)||
4 'spool off' as cmd
5 from (select 'i01_abc_omm' index_name from dual);
CMD
--------------------------------------------------
spool C:\Users\personal\MAIN_USR\index\i01_abc_omm
.txt --> see? Moved to the next line
SELECT DBMS_METADATA.GET_DDL('INDEX','i01_abc_omm'
,'MTO_INDX')
FROM DUAL;
spool off
That's why I'm not sure it'll actually help, but - you can try.
I'm trying save multiples txt archives with querye result using a single .sql archive called by a batch-file for automate-work.
My Batch-file:
set d=%location%result.txt
set f=%location%result2.txt
echo exit | sqlplus GOVMANPATCHORACLE/GOVMANPATCHORACLE#%SERVER%/%SERVICE% #C:\Users\enrique.erbs\Desktop\JOB\Tools\extrator_parametros\queryoracle.sql '%d%' '%f%'
*'%d%' contains the save locale to spool
My archive .sql (queryoracle.sql):
SET PAGESIZE 50000
SET LINESIZE 800
SET VERIFY OFF
SET ECHO ON
SET HEAD ON
SET SERVEROUTPUT ON;
SET PAGES 0
SET DEFINE ON;
COLUMN COD_PARAMETRO FORMAT a100 HEADING COD_PARAMETRO;
COLUMN DES_PARAMETRO FORMAT a100 HEADING DES_PARAMETRO;
COLUMN DES_VALOR FORMAT a100 HEADING DES_VALOR;
DECLARE
myvar1 varchar2(30);
myvar2 varchar2(30);
BEGIN
myvar1 := '$1';
myvar2 := '$2';
SPOOL 'myvar1'
SELECT TDFE_PARAMETRO_APLICACAO.COD_PARAMETRO AS COD_PARAMETRO, TDFE_PARAMETRO.DES_PARAMETRO AS DES_PARAMETRO, TDFE_PARAMETRO_APLICACAO.DES_VALOR AS DES_VALOR FROM TDFE_PARAMETRO_APLICACAO INNER JOIN TDFE_PARAMETRO ON(TDFE_PARAMETRO_APLICACAO.COD_PARAMETRO=TDFE_PARAMETRO.COD_PARAMETRO);
SPOOL OFF
SPOOL 'myvar2'
SELECT * FROM TDFE_PARAMETRO_APLICACAO;
SPOOL OFF
END;
I tryed some options and the most closest to get the result what i want is using ">> %location%\result.txt" at end of sqlplus call.
Somebody can help me ?
You were not far at all. Actually, When executing SQLPLUS and passing him a file to execute and arguments, It will replace every single occurence of &1 By your first Argument, Every occurence of &2 by your second argument, etc ...
This should work :
SET PAGESIZE 50000
SET LINESIZE 800
SET VERIFY OFF
SET ECHO ON
SET HEAD ON
SET SERVEROUTPUT ON;
SET PAGES 0
SET DEFINE ON;
COLUMN COD_PARAMETRO FORMAT a100 HEADING COD_PARAMETRO;
COLUMN DES_PARAMETRO FORMAT a100 HEADING DES_PARAMETRO;
COLUMN DES_VALOR FORMAT a100 HEADING DES_VALOR;
SPOOL '&1'
SELECT TDFE_PARAMETRO_APLICACAO.COD_PARAMETRO AS COD_PARAMETRO, TDFE_PARAMETRO.DES_PARAMETRO AS DES_PARAMETRO, TDFE_PARAMETRO_APLICACAO.DES_VALOR AS DES_VALOR FROM TDFE_PARAMETRO_APLICACAO INNER JOIN TDFE_PARAMETRO ON(TDFE_PARAMETRO_APLICACAO.COD_PARAMETRO=TDFE_PARAMETRO.COD_PARAMETRO);
SPOOL OFF
SPOOL '&2'
SELECT * FROM TDFE_PARAMETRO_APLICACAO;
SPOOL OFF
For reference : https://docs.oracle.com/cd/E11882_01/server.112/e16604/ch_twelve044.htm#SQPUG127
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 want to pass a variable from a Windows Batch File to SQLPLUS,
while displaying sql result batch variable should print along with the sql result.
Result should be stored in csv file.
How can I do that.
This is possible in Unix(shell script) how can I do that in Windows(batch script).
I want to pass a variable from a Windows Batch File to SQLPLUS
Just pass it as an argument to the SQL script. And use substitution variables in the same order that of the arguments list &1 &2...
For example,
mybatchfile.BAT:
sqlplus -S username/password#sid
#c:\sql\mysqlfile.sql 1000 7369
mysqlfile.SQL:
update emp set sal = &1 where empno = &2
while displaying sql result batch variable should print along with the sql result.
To display the variables which you pass as arguments to the SQL script, first you need to define the bind variables, then assign the argument value and then print the value.
For example,
I have test.sql:
For NUMBER type
-- define the bind variable
var sal number
-- assign the first argument's value
exec :sal := &1
-- display the value
print :sal
-- define the bind variable
var empno number
-- assign the second argument's value
exec :empno := &2
-- display the value
print :empno
Now, let's test the script:
SQL> #D:\test.sql 1000 7369
PL/SQL procedure successfully completed.
SAL
----------
1000
PL/SQL procedure successfully completed.
EMPNO
----------
7369
SQL>
For STRING type
-- define the bind variable
var ename varchar2(10)
-- assign the argument's value
exec :ename := '&1'
-- display the value
print :ename
Now, let's test the script:
SQL> #D:\test.sql LALIT
PL/SQL procedure successfully completed.
ENAME
--------------------------------
LALIT
SQL>
Result should be stored in csv file.
Handle this in the SQL script. Use proper SQL*Plus formatting for comma separated result. To store the result set, you just need to SPOOL
For example,
set colsep , -- separate columns with a comma
set pagesize 0 -- No headers
set trimspool on -- remove trailing blanks
spool mycsvfile.csv
SELECT ....
spool off