oracle begin开始end块中的PL/SQL游标声明

sigwle7e  于 2023-06-22  发布在  Oracle
关注(0)|答案(1)|浏览(143)

我正在Oracle中编写PL/SQL脚本。我需要声明一个游标,只有在诊断包被激活时才能在DBA_HIST_* 表上工作。我是说如果:

select sum(detected_usages)
from DBA_FEATURE_USAGE_STATISTICS where name in ('ADDM','AWR Baseline','AWR Baseline Template','AWR Report','Automatic Workload Repository','Baseline Adaptive Thresholds','Baseline Static Computations','Diagnostic Pack','EM Performance Page');

不大于0
我必须声明类似(简化)的内容:

DECLARE
CURSOR main_metrics_cursor IS
with main_metrics as 
(
    select snap_id, end_time, round(average/100,2) cpu_per_s
    from(
            --Si recuperano le metriche necessarie filtrando su dbid e considerando solo gli snap_id recuperati precedentemente
            select snap_id, end_time, instance_number, metric_name, average, maxval
            from DBA_HIST_sysmetric_summary
            and metric_name in ('CPU Usage Per Sec')
        )
    group by snap_id, end_time
    order by snap_id, end_time
)
select * from main_metrics;
metric_row main_metrics_cursor%ROWTYPE;

问题是只有在declare(没有打开游标阶段)Oracle才知道我使用了dba_hist视图。
我需要这个声明依赖于第一个查询结果(如果组件是授权的),否则我需要执行另一个代码,但我只能在开始/end块中使用if,并且声明必须在之前完成。
有什么想法吗
我需要DECLARE块依赖于if条件

llmtgqce

llmtgqce1#

听起来您不想声明静态游标,而是想使用动态SQL来打开基于查询结果的动态SQL语句的游标

declare
  l_rc sys_refcursor;
  l_cnt integer;
begin
  select sum(detected_usages) 
    into l_cnt 
    from DBA_FEATURE_USAGE_STATISTICS 
  where name in ('ADDM','AWR Baseline','AWR Baseline Template',
                 'AWR Report','Automatic Workload Repository',
                 'Baseline Adaptive Thresholds',
                 'Baseline Static Computations',
                 'Diagnostic Pack',
                 'EM Performance Page');

  if( l_cnt > 0 )
  then 
    open l_rc for 'with main_metrics ...';
 
    <<do something with l_rc>>
  end if;
end;

相关问题