7

I wan to create random data in Oracle table:

CREATE TABLE EVENTS(
  EVENTID INTEGER NOT NULL,
  SOURCE VARCHAR2(50 ),
  TYPE VARCHAR2(50 ),
  EVENT_DATE DATE,
  DESCRIPTION VARCHAR2(100 )
)
/

I tried this:

BEGIN  
FOR loop_counter IN 1..1000 
LOOP 
INSERT INTO EVENTS (EVENTID, SOURCE, TYPE, EVENT_DATE, DESCRIPTION) VALUES (loop_counter, loop_counter, 'warning', 
DBMS_RANDOM.VALUE(TO_CHAR(DATE '2000-01-01','J') ,TO_CHAR(DATE '9999-12-31','J')), dbms_random.value(1,100)); 
END LOOP; 
COMMIT; 
END;

I get this error exception

Error report - ORA-06550: line 5, column 13: PL/SQL: ORA-00932: inconsistent datatypes: expected DATE got NUMBER ORA-06550: line 4, column 1: PL/SQL: SQL Statement ignored 06550. 00000 - "line %s, column %s:\n%s" *Cause: Usually a PL/SQL compilation error. *Action:

Can you give me advice how I can fix this issue?

3
  • Yes, I updated the post. Commented Nov 22, 2015 at 21:38
  • That dbms_random() call will also not return a date, so it will fail if you try to insert a number like 3684482.0997 into a DATE column Commented Nov 22, 2015 at 21:39
  • 1
    How I have to update it? Commented Nov 22, 2015 at 21:39

3 Answers 3

16

Use:

BEGIN  
FOR loop_counter IN 1..1000 LOOP 
INSERT INTO "EVENTS" (EVENTID, "SOURCE", TYPE, EVENT_DATE, DESCRIPTION) 
VALUES (loop_counter, loop_counter, 'warning', 
        TO_DATE(TRUNC(DBMS_RANDOM.VALUE(TO_CHAR(DATE '2000-01-01','J') ,TO_CHAR(DATE '9999-12-31','J'))),'J')
        ,dbms_random.value(1,100)
       ); 
END LOOP; 
COMMIT; 
END;

SqlFiddleDemo

Changes:

  1. Add mising ; after final END
  2. Quote keywords
  3. Rewrite random date generation
Sign up to request clarification or add additional context in comments.

Comments

4
INSERT INTO EVENTS (EVENTID, "SOURCE", TYPE, EVENT_DATE, DESCRIPTION)
SELECT level, level, 'warning', 
        TO_DATE(TRUNC(DBMS_RANDOM.VALUE(TO_CHAR(DATE '2000-01-01','J') ,TO_CHAR(DATE '9999-12-31','J'))),'J')
        ,dbms_random.value(1,100)
  FROM DUAL
CONNECT BY LEVEL <= 1000;

Comments

3

Also, if you use PL/SQL Developer by Allroundautomations, you can find out good tool for this job: Data Generator. It can be very useful, because it can help to generate some data in any types and place it to a tables.

(see screenshot attached) enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.