简体   繁体   中英

Oracle generate insert statements

I have about 10 tables that I am selecting from with a select and join statement. I got the select statement working but want to generate more data to ensure that it really works.

Is there an easy way to generate dummy data with out doing it manually. That is if select from table a join table b on a.id = b.id . How can I generate data that will meet this requirement without doing it manually? If this doesn't make sense I can make it more clearly. Thanks!!

Simple example using LEVEL to insert 10 rows - run inner query to see results:

INSERT INTO emp_test(empno)
(
 SELECT 1+LEVEL-1 FROM dual
  CONNECT BY LEVEL <= 10
)
/  

For related tables (where you need joins and referential integrity to work..) You'll have to write a pl/sql block or a set of inserts one after the other.

PL/SQL : --see below to see how to generate multiple rows...

Begin
  for v_rec in (select level from dual
                 connect by level <= 1000)
  loop
     create_new_dept (v_rec.level);
     create_1000_rows_for_dept(v_rec.level);
  end loop;
end;
/

or SQL:

--create 1000 departments.
insert into dept(deptno, dname)
select level, 'deptno ' || level
from dual
connect by level <= 1000;
commit;

--create 1000 employees for each department..
with emp as
(select level empid
  from dual
  connect by level <=1000
 )
insert into emp(empno, deptno, dname)
select deptno,
       emp_seq.nextval,
       'dname ' || 'emp ' || level empname
  from dept,
       emp; --no join condition, cartesian join on purpose
commit;

There are two common approaches when the tables are not related to one another.

Using DBMS_RANDOM, connect by queries and generating multiple rows. example...

--This will insert 1000 rows {(1, emp1), (2,emp2)} and so on..
insert into emp (empno, ename)
select level, 'employee ' || level
from emp
connect by level <=1000;

Using a table like dba_objects and doing multiple inserts to generate data. This is useful if you want to test a case like "performance of function based indexes)" and don't really care what data it is, as long as there is a lot of it.

create table my_test_table
as
select * from dba_objects; --or user_objects, all_objects

--keep doubling rows until you have enough rows.
insert into my_test_table
select * from my_test_table;
commit;

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