Pages

Thursday, December 17, 2015

Data generation for oracle

https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:2151576678914


create or replace procedure clone( p_tname in varchar2, p_records in number )
  authid current_user
  as
      l_insert long;
      l_rows   number default 0;
  begin

     execute immediate 'create table clone_' || p_tname ||
                       ' as select * from ' || p_tname ||
                       ' where 1=0';

      l_insert := 'insert into clone_' || p_tname ||
                  ' select ';

      for x in ( select data_type, data_length,
                  rpad( '9',data_precision,'9')/power(10,data_scale) maxval
                   from user_tab_columns
                  where table_name = 'CLONE_' || upper(p_tname)
                  order by column_id )
      loop
          if ( x.data_type in ('NUMBER', 'FLOAT' ))
          then
              l_insert := l_insert || 'dbms_random.value(1,' || x.maxval ||
                                                                        '),';
          elsif ( x.data_type = 'DATE' )
          then
              l_insert := l_insert ||
                    'sysdate+dbms_random.value+dbms_random.value(1,1000),';
          else
              l_insert := l_insert || 'dbms_random.string(''A'',' ||
                                         x.data_length || '),';
          end if;
      end loop;
      l_insert := rtrim(l_insert,',') ||
                    ' from all_objects where rownum <= :n';

      loop
          execute immediate l_insert using p_records - l_rows;
          l_rows := l_rows + sql%rowcount;
          exit when ( l_rows >= p_records );
      end loop;
  end;
  /

grant create procedure to user1;
grant execute any procedure to user1;

scott@TKYTE9I.US.ORACLE.COM> exec clone( 'emp', 5 );

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.01
scott@TKYTE9I.US.ORACLE.COM> select * from clone_emp;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ------
      4923 BrCyhfdHaQ mGSqkWMvy       8302 07-SEP-02    2765.67   89231.32     27
       323 ImuBZCYDrt TdjoflvYE       9613 11-AUG-03   82158.44   34478.25     89
      8773 jnTPtKkchC KzUezmTTL       8432 20-MAY-04    29909.7   23860.67     24
      7374 aETDfeptSS ObVBgtnAP       9033 28-MAR-03   34012.77   92188.64     45
      4530 MKGvauUMOQ GXwcRjGUn         51 07-AUG-04    84510.9   93629.66     79

No comments:

Post a Comment