oracle大批量添加测试数据实例

一、Test Windos方式
declare

  maxrecords constant int:=100000;

  i int :=1;

  begin

  for i in 1..maxrecords loop

  insert into test2

  (id, name)

  values

  (test2_seq.nextval, to_char(9999+i));

  end loop;

  dbms_output.put_line(' 成功录入数据!');

  commit;

  end;

 二、从已有表中往入另一张表导数据
create or replace procedure TestProc is

  begin

  for c in(select id, name from test2) loop

  insert into test

  (id,

  name)

  values

  (test_seq.nextval, c.name);

  end loop;

  end TestProc;


declare
  TYPE struct_array_callerNo IS TABLE OF varchar2(40) INDEX BY BINARY_INTEGER;
  callerNos struct_array_callerNo;
  TYPE struct_array_calleeNo IS TABLE OF varchar2(40) INDEX BY BINARY_INTEGER;
  calleeNos struct_array_calleeNo;
  TYPE struct_array_staffId IS TABLE OF varchar2(40) INDEX BY BINARY_INTEGER;
  staffIds  struct_array_staffId;
  TYPE struct_array_workNo IS TABLE OF varchar2(40) INDEX BY BINARY_INTEGER;
  workNos   struct_array_workNo;
 
  idx_callerNo int := 1;
  idx_calleeNo int := 1;
  idx_staffId  int := 1;
  idx_workNo   int := 1;

  theDate date := to_date('2012-01-01 08:08:08', 'yyyy-mm-dd hh24:mi:ss');

  maxrecords constant int := 10;
  i            int := 1;
  tmp int := 1;
 
begin
  i := 1;
  for c in (select distinct (caller_no) field from t_cct_rec) loop
    callerNos(i) := c.field;
    i := i + 1;
  end loop;

  i := 1;
  for c in (select distinct (callee_no) field from t_cct_rec) loop
    calleeNos(i) := c.field;
    i := i + 1;
  end loop;

  i := 1;
  for c in (select distinct (staff_id) field from t_cct_rec) loop
    staffIds(i) := c.field;
    i := i + 1;
  end loop;

  i := 1;
  for c in (select distinct (work_no) field from t_cct_rec) loop
    workNos(i) := c.field;
    i := i + 1;
  end loop;
 
  dbms_output.put_line('callerNos.count:'||callerNos.count);
  dbms_output.put_line('calleeNos.count:'||calleeNos.count);
  dbms_output.put_line('staffIds.count:'||staffIds.count);
  dbms_output.put_line('workNos.count:'||workNos.count);

  i := 1;
  for i in 1 .. maxrecords loop
    idx_callerNo := ceil(DBMS_RANDOM.VALUE(1, callerNos.count));
    idx_calleeNo := ceil(DBMS_RANDOM.VALUE(1, calleeNos.count));    
    idx_staffId := ceil(DBMS_RANDOM.VALUE(1, staffIds.count));    
    idx_workNo := ceil(DBMS_RANDOM.VALUE(1, workNos.count));

    insert into t_cct_rec(ID,
                   CALLER_NO,
                   CALLEE_NO,
                   STAFF_ID,
                   WORK_NO,
                   CALLSTARTTIME,
                   CALLENDTIME,
                   EXT1) values(seq_cctid.nextval,
                                callerNos(idx_callerNo),
                                calleeNos(idx_calleeNo),
                                staffIds(idx_staffId),
                                workNos(idx_workNo),
                                theDate + numtodsinterval(DBMS_RANDOM.VALUE(1, 365 * 24 * 60), 'minute'),
                                theDate + numtodsinterval(DBMS_RANDOM.VALUE(1, 365 * 24 * 60) + 2, 'minute'),
                                MOD(i, 2));

  end loop;

  dbms_output.put_line('成功录入数据!');
  commit;
end;

你可能感兴趣的:(oracle大批量添加测试数据实例)