简体   繁体   中英

How to execute 'Select' query on all Tables within a schema on Oracle

I have a schema which is having more than 1000 tables, I need to run 'Select' query ie

SELECT * FROM Table_Name

On each and every table of a schema, is this possible or not? if Yes, then How?

Sure it's possible. You would write 1,000+ statements, and then run them. Or maybe you write a script that dynamically generates the SQL for you, and you run that.

But there's only one reason I could even imagine why you would want to do something like this, and that's to EXPORT the entire schema from the database, and move that data somewhere else.

And IF that's what you want to do, then there are MUCH better ways of doing it ( Data Pump ).

What you are proposing will be tremendously slow, and overwhelm whatever client/program/display you are using.

Update your question with some 'why' or business requirements, and we can give you a much better answer.

You're doing it wrong, apparently. I, as the others, can't see a valid reason doing it that way but - as you've said - you asked a question and need an answer. So, here's one option to do that: it is based on Scott's schema which contains several tables.

  • I'm purging recyclebin to get rid of garbage
  • Select from TAB shows which tables I expect to get as the output
  • PL/SQL, in a loop, creates set of begin spool into a TXT file - select statement itself - end spooling SQL*Plus commands
  • DBMS_OUTPUT result should be copy/pasted into SQL*Plus and executed
  • the final result is list of TXT files which contain data
  • I'm setting LINESIZE and PAGESIZE so that the output looks prettier than usual; you might want to set some other parameters to. Check the SQL*Plus documentation for more info

OK, here you go:

SQL> purge recyclebin;

Recyclebin purged.

SQL> select * from tab;

TNAME                          TABTYPE  CLUSTERID
------------------------------ ------- ----------
BONUS                          TABLE
DEPT                           TABLE
EMP                            TABLE
SALGRADE                       TABLE

SQL> set serveroutput on;
SQL> begin
  2    for cur_r in (select tname from tab) loop
  3      dbms_output.put_line('spool ' || cur_r.tname ||'.txt');
  4      dbms_output.put_line('select * from ' || cur_r.tname ||';');
  5      dbms_output.put_line('spool off');
  6    end loop;
  7  end;
  8  /
spool BONUS.txt
select * from BONUS;
spool off
spool DEPT.txt
select * from DEPT;
spool off
spool EMP.txt
select * from EMP;
spool off
spool SALGRADE.txt
select * from SALGRADE;
spool off

PL/SQL procedure successfully completed.

SQL>

Now, running that result:

SQL> set pagesize 100
SQL> set linesize 100
SQL> spool BONUS.txt
SQL> select * from BONUS;

ENAME      JOB              SAL       COMM
---------- --------- ---------- ----------
KING       PRESIDENT       1000        100

SQL> spool off
SQL> spool DEPT.txt
SQL> select * from DEPT;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON

SQL> spool off
SQL> spool EMP.txt
SQL> select * from EMP;

     EMPNO ENAME      JOB              MGR HIREDATE        SAL       COMM     DEPTNO
---------- ---------- --------- ---------- -------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17.12.80        800                    20
      7499 ALLEN      SALESMAN        7698 20.02.81       1600        300         30
      7521 WARD       SALESMAN        7698 22.02.81       1250        500         30
      7566 JONES      MANAGER         7839 02.04.81       2975                    20
      7654 MARTIN     SALESMAN        7698 28.09.81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01.05.81       2850                    30
      7782 CLARK      MANAGER         7839 09.06.81       2450                    10
      7839 KING       PRESIDENT            17.11.81       5000                    10
      7844 TURNER     SALESMAN        7698 08.09.81       1500          0         30
      7900 JAMES      CLERK           7698 03.12.81        950                    30
      7902 FORD       ANALYST         7566 03.12.81       3000                    20
      7934 MILLER     CLERK           7782 23.01.82       1300                    10

12 rows selected.

SQL> spool off
SQL> spool SALGRADE.txt
SQL> select * from SALGRADE;

     GRADE      LOSAL      HISAL
---------- ---------- ----------
         1        700       1200
         2       1201       1400
         3       1401       2000
         4       2001       3000
         5       3001       9999

SQL> spool off
SQL>

In current directory, there are now several TXT files:

SQL> $dir *.txt
 Volume in drive C is OSDisk
 Volume Serial Number is 7635-F892

 Directory of C:\Users\littlefoot

16.08.2018.  21:12               353 BONUS.txt
16.08.2018.  21:12               658 DEPT.txt
16.08.2018.  21:12             1.494 EMP.txt
16.08.2018.  21:12               764 SALGRADE.txt
               4 File(s)          3.269 bytes
               0 Dir(s)  304.432.480.256 bytes free

SQL>

Now, you're free to use them the best you can.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM