我想在Oracle表中创建随机数据:
CREATE TABLE EVENTS(
EVENTID INTEGER NOT NULL,
SOURCE VARCHAR2(50 ),
TYPE VARCHAR2(50 ),
EVENT_DATE DATE,
DESCRIPTION VARCHAR2(100 )
)
/
我试过了:
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;
我收到此错误异常
错误报告 - ORA-06550:第5行第13列:PL / SQL:ORA-00932: 不一致的数据类型:预期DATE得到NUMBER ORA-06550:第4行, 第1列:PL / SQL:忽略SQL语句 06550. 00000 - "行%s,列%s:\ n%s" *原因:通常是PL / SQL编译错误。 *操作:
你能否告诉我如何解决这个问题?
答案 0 :(得分:7)
使用:
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
强>
的变化:
;
END
答案 1 :(得分:2)
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;
答案 2 :(得分:1)