我試圖生成樣本資料,因此我想在我的Oracle表中插入多行。列的值應該在1-5之間,而且應該是完全隨機的。我需要20個隨機的整數。
我的插入陳述句:
BEGIN。
FOR i IN 163 ... 400 LOOP
INSERT ALL INTO results
(student_id,OPN1,OPN2,OPN3,OPN4,AGG1,AGG2,AGG3,AGG4,NEU1,NEU2,NEU3,NEU4,EXT1,EXT2,EXT3,EXT4,CSN1,CSN2,CSN3,CSN4)
VALUES[/span
(i,
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5))
SELECT * FROM DUAL;
END LOOP;
COMMIT;
END;
它可以作業,但在一行中,數字都是一樣的。它沒有為每一列生成一個新的亂數,只是生成了一個,并將其插入到我的表的所有欄位。為什么會這樣呢?你可以在102 id行之后看到它。第一個插入的行(163 id)已經錯了。
uj5u.com熱心網友回復:
INSERT ALL有一些怪癖,但你在這里不需要它,你可以直接洗掉ALL和假的select,使其成為一個簡單的insert-values陳述句:
BEGIN
FOR i IN 163 ... 400 LOOP
INSERT INTO results
(student_id,OPN1,OPN2,OPN3,OPN4,AGG1,AGG2,AGG3,AGG4,NEU1,NEU2,NEU3,NEU4,EXT1,EXT2,EXT3,EXT4,CSN1,CSN2,CSN3,CSN4)
VALUES[/span
(i,
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5) )。
END LOOP;
COMMIT;
END;
/
不過你也不需要PL/SQL,你可以使用分層查詢(或遞回CTE)來代替回圈:
INSERT INTO results
(student_id,OPN1,OPN2,OPN3,OPN4,AGG1,AGG2,AGG3,AGG4,NEU1,NEU2,NEU3,NEU4,EXT1,EXT2,EXT3,EXT4,CSN1,CSN2,CSN3,CSN4)
SELECT[/span
等級 162,
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)。
round(dbms_random.value(1,5)
FROM dual
CONNECT BY level < = 1 400 - 163;
或者按照@MTO的建議,用floor代替round:
INSERT INTO results
(student_id,OPN1,OPN2,OPN3,OPN4,AGG1,AGG2,AGG3,AGG4,NEU1,NEU2,NEU3,NEU4,EXT1,EXT2,EXT3,EXT4,CSN1,CSN2,CSN3,CSN4)
SELECT[/span
等級 162。
floor(dbms_random.value(1,6)。
floor(dbms_random.value(1,6) )。)
floor(dbms_random.value(1,6) )。)
floor(dbms_random.value(1,6) )。)
floor(dbms_random.value(1,6) )。)
floor(dbms_random.value(1,6) )。)
floor(dbms_random.value(1,6) )。)
floor(dbms_random.value(1,6) )。)
floor(dbms_random.value(1,6) )。)
floor(dbms_random.value(1,6) )。)
floor(dbms_random.value(1,6) )。)
floor(dbms_random.value(1,6) )。)
floor(dbms_random.value(1,6) )。)
floor(dbms_random.value(1,6) )。)
floor(dbms_random.value(1,6) )。)
floor(dbms_random.value(1,6) )。)
floor(dbms_random.value(1,6) )。)
floor(dbms_random.value(1,6) )。)
floor(dbms_random.value(1,6) )。)
floor(dbms_random.value(1,6)
FROM dual
CONNECT BY level < = 1 400 - 163;
db<>fiddle(為簡化而減少列數)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/324994.html
標籤:
