Quantcast
Channel: Selecting from random amount of tables with identical structure - Database Administrators Stack Exchange
Viewing all articles
Browse latest Browse all 3

Answer by Balazs Papp for Selecting from random amount of tables with identical structure

$
0
0

I am not proud of publicly writing something like this.

Sample + data:

CREATE TABLE donaldduck ( SURNAME VARCHAR(100), TOWN VARCHAR(100) );CREATE TABLE daisyduck  ( SURNAME VARCHAR(100), TOWN VARCHAR(100) );CREATE TABLE goofy      ( something number(1), SURNAME VARCHAR(100), TOWN VARCHAR(100) );INSERT INTO DONALDDUCK ( SURNAME, TOWN ) VALUES ( 'Dagobert Duck', 'Entenhausen' );INSERT INTO daisyduck ( SURNAME, TOWN ) VALUES ( 'Daisy Duck', 'Entenhausen' );INSERT INTO goofy ( SOMETHING, SURNAME, TOWN ) VALUES (   1, 'Goofy Dog', 'Entenhausen' );commit;

Types and PL/SQL using a pipelined function:

create type t_NAME_OF_USER as object(    SURNAME VARCHAR(100),    TOWN VARCHAR(100));/create type t_name_of_user_tab IS TABLE OF t_NAME_OF_USER;/CREATE OR REPLACE FUNCTION get_surname_town RETURN t_name_of_user_tab PIPELINED AS  rc sys_refcursor;  query clob;  l_surname varchar2(100);  l_town varchar2(100);begin  for t in (    select table_name from user_tables ut    where'SURNAME' in (select column_name from user_tab_columns utc where utc.table_name = ut.table_name) and'TOWN'  in (select column_name from user_tab_columns utc where utc.table_name = ut.table_name)    )  loop    open rc for 'select surname, town from ' || t.table_name ;    loop      fetch rc into l_surname, l_town;      exit when rc%notfound;      pipe row(t_NAME_OF_USER(l_surname, l_town));    end loop;  end loop;end;/

SELECT:

select * from table(get_surname_town);SURNAME                        TOWN------------------------------ --------------------Daisy Duck                     EntenhausenDagobert Duck                  EntenhausenGoofy Dog                      Entenhausen

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>