【发布时间】:2015-11-10 21:49:36
【问题描述】:
我想生成随机数据到Oracle表中:
第一张桌子
-- TABLE AGENT_HISTORY
CREATE TABLE AGENT_HISTORY(
EVENT_ID INTEGER NOT NULL,
AGENTID INTEGER NOT NULL,
EVENT_DATE DATE NOT NULL
)
/
CREATE INDEX IX_RELATIONSHIP1 ON AGENT_HISTORY (AGENTID)
/
-- ADD KEYS FOR TABLE AGENT_HISTORY
ALTER TABLE AGENT_HISTORY ADD CONSTRAINT KEY8 PRIMARY KEY (EVENT_ID)
/
第二张表
-- TABLE CPU_HISTORY
CREATE TABLE CPU_HISTORY(
CPU_HISTORY_ID INTEGER NOT NULL,
EVENT_ID INTEGER NOT NULL,
CPU_NAME VARCHAR2(50 ) NOT NULL,
CPU_VALUE NUMBER NOT NULL
)
/
我试图创建这个 PL/SQL 块:
Error starting at line : 1,690 in command -
BEGIN
FOR loop_counter IN 1..1000
LOOP
INSERT INTO CPU_HISTORY_ID (CPU_HISTORY_ID, EVENT_ID, CPU_NAME, CPU_VALUE) VALUES (loop_counter, loop_counter, 'cpu1', dbms_random.value(1,100));
END LOOP;
COMMIT;
END;
错误
Error report -
ORA-06550: line 5, column 16:
PL/SQL: ORA-00942: table or view does not exist
ORA-06550: line 5, column 4:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Error starting at line : 1,680 in command -
第二个 PL/SQL 块
BEGIN
FOR loop_counter IN 1..1000
LOOP
INSERT INTO AGENT_HISTORY (EVENT_ID, AGENTID, EVENT_DATE) VALUES (loop_counter, 22, SYSDATE);
END LOOP;
COMMIT;
END;
错误
Error report -
ORA-00001: unique constraint (ADMIN.KEY8) violated
ORA-06512: at line 5
00001. 00000 - "unique constraint (%s.%s) violated"
*Cause: An UPDATE or INSERT statement attempted to insert a duplicate key.
For Trusted Oracle configured in DBMS MAC mode, you may see
this message if a duplicate entry exists at a different level.
*Action: Either remove the unique restriction or do not insert the key.
你能在我错的地方给我一些建议吗?
还有其他方法可以在表中生成 1000 行随机值吗?
【问题讨论】: