如何将绑定变量的值赋予host(define)变量?

时间:2015-11-21 13:29:09

标签: sql oracle plsql

logistf

然后如果我希望variable dbid number; select dbid into :dbid from dba_hist_snapshot; 可以作为主变量的值,例如:

:dbid

如何制作?

其实我只想执行@@?/ rdbms / admin / awrsqrpt.sql;没有交互式命令行。

2 个答案:

答案 0 :(得分:0)

这显示了如何运行报告。 sqlplus变量(非绑定变量)前面有一个&符号,可以使用带有NEW_VALUE或OLD_VALUE选项的COLUMN语句进行设置。更多信息可以在Oracle SQL*Plus User's Guide and Reference

中找到
rem check ?/rdbms/admin/awrrpt.sql
rem and check ?/rdbms/admin/awrrpti.sql
rem we want not display a list of snap_ids
define num_days = 0;
rem we want to create a report of type text
define report_type='text';

rem choose the dbid of the current instance
column dbid old_value dbid;
select dbid from v$database;

rem choose the instance_number of the current instance for inst_num
column INSTANCE_NUMBER old_value inst_num
select INSTANCE_NUMBER from v$instance;


rem choose the last snap_id as END_SNAP
column end_snap new_value end_snap
select max(snap_id) end_snap
      from DBA_HIST_SNAPSHOT
  where DBID=&DBID
  and INSTANCE_NUMBER=&INST_NUM
/

rem choose the snap_id before the last snap_id as BEGIN_SNAP
column begin_snap new_value begin_snap
select max(snap_id) begin_snap
      from DBA_HIST_SNAPSHOT
  where DBID=&DBID
  and snap_id < &end_snap
  and INSTANCE_NUMBER=&INST_NUM
/

rem the following ways to define report_name do not work
rem because of leading blanks in the variables
rem define report_name=awr_&BEGIN_SNAP._&END_SNAP..txt
rem define report_name="awr_&BEGIN_SNAP._&END_SNAP..txt"

REM set the report name
column report_name old_value report_name
select 'awr_'||trim('&BEGIN_SNAP')||'_'||
  trim('&END_SNAP')||'.txt' report_name
from dual;

promp report_name &report_name
@?/rdbms/admin/awrrpt.sql

exit

答案 1 :(得分:0)

-- get dbid, inst_num, bid, eid, sql_id for awr report
var dbid number;
var inst_num number;
var bid number;
var eid number;
var sql_id varchar2(20);
begin
    select dbid into :dbid from v$database;
    select instance_number into :inst_num from v$instance;
    select min(snap_id) into :bid from dba_hist_snapshot;
    select max(snap_id) into :eid from dba_hist_snapshot;
    :sql_id := 'xxxxx';
end;
/

-- create awr for SQL ID: 1swzr1yyxu1q0 to save at '/tmp', report name awr_.html
CREATE OR REPLACE DIRECTORY awr_reports_dir AS '/tmp'; 
--'&1'
declare
l_dir varchar2(50) := 'AWR_REPORTS_DIR';
l_file UTL_FILE.file_type;
l_file_name varchar(50);
begin
    l_file := utl_file.fopen (l_dir, 'awr_'||:sql_id||'.html', 'w', 32767);
    for cur_rep in (select output from table(dbms_workload_repository.awr_sql_report_html(:dbid, :inst_num,:bid,:eid, :sql_id,0 )))
    loop
        utl_file.put_line(l_file, cur_rep.output);
    end loop;
    utl_file.fclose(l_file);
end;
/