Quick Search:
 
 Oracle PL/SQL: Create test data Jump to:  
Category: >> Oracle PL/SQL >> Create test data  

<< lastnext >>

Snippet Name: Create test data

Description: Instructions:

Copy the create table ddl into SQL/Plus.

Set the number of records you wish to create in the rec_num_var.

Copy the create procedure ddl into SQL/Plus.

In SQL/Plus type "exec datagen_proc" (without the quotes).

Comment: (none)

Language:
Highlight Mode: PLSQL
Last Modified: February 27th, 2009

 
CREATE TABLE table_x 
(id     NUMBER,
txt1      VARCHAR2(10),
date1     DATE,
num1     NUMBER);
 
 
CREATE OR REPLACE PROCEDURE datagen_proc AS
 
num_var          INTEGER;
txt_var          VARCHAR2(10);
date_var     DATE;
id_var           INTEGER;
i           INTEGER;
counter      INTEGER;
rec_num_var       INTEGER;
 
BEGIN
 
rec_num_var     := 100;          -- set the number of records you want
num_var          := 9;
txt_var          := 'z';
date_var     := TO_DATE('01-JAN-2001');
id_var           := 0;
counter      := 1;
 
EXECUTE IMMEDIATE 'truncate table table_x';
 
FOR i IN 1.. rec_num_var          
 
LOOP
     counter := counter +1;
     id_var := id_var+1;
 
     SELECT DECODE (txt_var, 'z', 'a',
               'a', 'b',
               'b', 'c',
               'c', 'a', 'error')
     INTO txt_var
     FROM dual;
 
     SELECT DECODE (num_var, 9, 1,
               1, 2,
               2, 3,
               3, 1)
     INTO num_var
     FROM dual;
 
     SELECT DECODE (date_var, TO_DATE('01-JAN-2001'), TRUNC(SYSDATE),
               TRUNC(SYSDATE), TRUNC(SYSDATE)-1,
               TRUNC(SYSDATE)-1, TRUNC(SYSDATE)-2,
               TRUNC(SYSDATE)-2, TRUNC(SYSDATE))
     INTO date_var
     FROM dual;
 
 
     INSERT INTO table_x (id, txt1, num1, date1)
     VALUES (id_var, txt_var, num_var, date_var);
 
     IF      counter = 100
     THEN      
          COMMIT;
          counter := 0;
     ELSE     NULL;
     END IF;
 
END LOOP;
 
COMMIT;
 
END datagen_proc;
 


 
   Home |    Search |    Code Library |    Sponsors |    Privacy |    Terms of Use |    Contact Us © 2003 - 2024 psoug.org