从每个模式的表中选择行

时间:2015-10-14 23:34:08

标签: sql oracle

我想从具有给定LIKE名称的每个模式中选择DATABASECHANGELOGLOCK表中的行。即...... LIKE'myschemaprefix%';

我想要UNIONS以外的东西,因为我有大量用户。

.block_photo:first-child, .block_video:first-child {
    margin-left:0px;
}

我使用过程来捕获所有模式的名称来攻击问题。

SELECT 'MySchema1' as SCHEMA_NAME, LOCKED, LOCKGRANTED, LOCKEDBY FROM MySchema1.DATABASECHANGELOGLOCK
UNION
SELECT 'MySchema2' as SCHEMA_NAME, LOCKED, LOCKGRANTED, LOCKEDBY FROM MySchema1.DATABASECHANGELOGLOCK;

我需要的是循环中的SELECT,它使用输出值来获取LOCKED = 1的任何行;

想法?

1 个答案:

答案 0 :(得分:0)

这可能会获取所需的数据

SET serveroutput ON
declare
  cur_locks sys_refcursor; -- cursor to fetch lock info

  -- variables to store data from a row of lock info
  v_locked number;
  v_lock_granted varchar2(30);
  v_locked_by varchar2(30);

BEGIN
  FOR item IN (
    SELECT USERNAME FROM dba_users WHERE USERNAME LIKE 'MySchemaPrefix%'
  ) LOOP

      OPEN cur_locks FOR
        'SELECT LOCKED, LOCKGRANTED, ' ||
        ' LOCKEDBY FROM '  || item.USERNAME  || '.DATABASECHANGELOGLOCK '
        || ' WHERE LOCKED = 1 ';       

      -- loop to fetch and print
      LOOP
        fetch cur_locks into 
         v_locked, v_lock_granted, v_locked_by;
        EXIT WHEN cur_locks%NOTFOUND;
        dbms_output.put_line(
          item.USERNAME ||','||
          v_locked ||','||
          v_lock_granted ||','||
          v_locked_by
        );
      end loop;

      close cur_locks;

    --DBMS_OUTPUT.PUT_LINE
      --(item.username||'.DATABASECHANGELOGLOCK');
  END LOOP;
END;
/