Home > database >  How to get a table by a field content split into multiple tables?
How to get a table by a field content split into multiple tables?

Time:09-29

Found to have similar problems, but essentially, oracle writing, for reference, to weigh the address http://bbs.csdn.net/topics/100034393
A table is as follows:
ID NAME CLASS
1 AA II
2 BB IV
3 AB IV
4 AC II
5 CC IX
6 SS II
7 AS IX
8 ES IX
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
With what method can be put on the table according to the content of the class is divided into multiple tables
The results are as follows:
ID NAME CLASS
1 AA II
4 AC II
6 SS II
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
ID NAME CLASS
2 BB IV
3 AB IV
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
ID NAME CLASS
5 CC IX
7 AS IX
8 ES IX

CodePudding user response:

 create table t1 as select * from t where class='2'; 
The create table t2 as select * from t where class="IV";
The create table t3 as select * from t where class='x';

The table name, after t1 t2, t3 is t is the source table,

CodePudding user response:

This is, for example, is a large amount of actual if class, how to do, and method for batch build table

CodePudding user response:

reference 1st floor NextAction response:
 create table t1 as select * from t where class='2'; 
The create table t2 as select * from t where class="IV";
The create table t3 as select * from t where class='x';

The table name, after t1 t2, t3 is t is the source table,

This is, for example, is a large amount of actual if class, how to do, and method for batch build table

CodePudding user response:

Using dynamic SQL cursor can

CodePudding user response:


Declare
V_tid int.
V_sql varchar (300);
The begin
V_tid:=0;
For x in (select distinct class from t) loop
V_tid:=v_tid + 1;
V_sql:='create table t' | | v_tid | | 'as select * from t where class=: 1'.
The execute immediate v_sql using x.c lass;
End loop;
The end;

CodePudding user response:

references 4 floor solid honest reply:
with dynamic SQL cursor can

Can go into more detail about it,,,

CodePudding user response:

 
DECLARE
CURSOR CR_CL IS the SELECT DISTINCT "CLASS" FROM T1;
KK CR_CL % ROWTYPE;
PP INTEGER;
V_SQL VARCHAR (2000);
The BEGIN
PP:=1;
The OPEN CR_CL;
LOOP
The FETCH CR_CL INTO KK.
EXIT the WHEN CR_CL % notfound;
V_SQL:='CREATE TABLE PA_' | | TO_CHAR (PP) | | 'SELECT * FROM T1 WHERE CLASS=KK';
The EXEC IMMEDIATE V_SQL;
END LOOP;
END;

  • Related