使用ASM磁盘组的in运算符连接SQL select语句

时间:2018-11-03 19:12:56

标签: sql oracle plsql disk-partitioning

很抱歉,如果这是一个愚蠢的问题,但我无法解决。我经常收到客户端的请求,要求从Diskgroup中删除一些磁盘。

所以在此过程中-我想从v$asm_diskv$asm_diskgroup生成动态sql。

SQL:

select d.path, d.FAILGROUP,d.os_mb/1024 GB
from v$asm_disk d, v$asm_diskgroup dg
where d.group_number = dg.group_number and dg.name='Test_archive'
order by DISK_GROUP_NAME;

动态sql输出应如下所示:

ALTER DISKGROUP Test_archive DROP DISK mnc1, mnc2, mnc3;

能否让我知道如何实现上述输出?

2 个答案:

答案 0 :(得分:1)

您可以使用以下机制

(但在使用立即执行之前,请谨慎处理此紧急危险操作,只需通过 dbms_output.put_line

SQL> set serveroutput on;
SQL> declare
  v_command varchar2(5000);
begin
    for c in 
    ( 
      select failgroup, listagg(name, ',') within group(order by name) as disks
        from (select d.failgroup, dg.name
                from v$asm_disk d
                join v$asm_diskgroup dg
                  on (d.group_number = dg.group_number)
               where dg.name = 'Test_archive'
               group by d.failgroup, dg.name)
       group by failgroup 
    )
    loop
     begin      
         v_command := 'ALTER DISKGROUP  '||c.failgroup||' DROP DISK '||c.disks||';';
         dbms_output.put_line( v_command );
        --execute immediate v_command; --firstly comment this line out to see
                                       --whether the result as you want, then
                                       --open for the operation.
      exception when others then 
        begin
         dbms_output.put_line( sqlerrm );
        end; 
     end;  
    end loop;
end;

答案 1 :(得分:0)

这对您有用吗?让我知道您是否想要解释什么:

select 
       'ALTER DISKGROUP Test_archive DROP DISK'
    || chr(10)
    || listagg(dg_name,','||chr(10)) within group (order by dg_name)
    ||';'
from
-- your query here, placeholder with test data:
(select 'mnc1' dg_name from dual union all select 'mnc2' from dual union all select 'mnc3' from dual)
;

输出:

ALTER DISKGROUP Test_archive DROP DISK
mnc1,
mnc2,
mnc3;