declare
maxrecords constant int:=1000;
i int :=1;
begin
for i in 1..maxrecords loop
insert into UserInfo(UserID,login,password,firstName,lastName,ISADMINISTRATOR)
values(SYS_GUID(),TO_CHAR('9999'+i),'password','fristname','lastName','0');
end loop;
dbms_output.put_line(' 成功錄入資料! ');
commit;
end;
SYS_GUID() 產生 GUID ORACLE
NEWID 產生 GUID SQLSERVERR
declare |
定義部分標識 |
maxrecords constant int:=100; |
定義 maxrecords 為整型常量 100 |
i int :=1; |
定義 i 為整型值變數,初值為 1 |
Begin |
執行部分標識 |
for i in 1..maxrecords loop |
i 從 1 迴圈到 maxrecords |
Insert into tempuser.testtable(recordnumber,currentdate) values (i,sysdate); |
向資料表中插入資料 |
end loop; |
結束迴圈 |
dbms_output.put_line(' 成功錄入資料! '); |
顯示成功錄入資料資訊 |
commit; |
提交結果 |
end; |
結束執行 |
用笛卡爾積
create table t as
(select * from dba_objects t1,dba_objects t2
where rownum<10000000)
注意先分配好資料表空間
做資料庫開發或管理的人經常要建立大量的測試資料,動不動就需要上萬條,如果一條一條的錄入,那會浪費大量的時間,本文介紹了Oracle中如何通過一條SQL快速產生大量的測試資料的方法。
產生測試資料的SQL如下:
SQL> select rownum as id,
2 to_char(sysdate + rownum / 24 / 3600, 'yyyy-mm-dd hh24:mi:ss') as inc_datetime,
3 trunc(dbms_random.value(0, 100)) as random_id,
4 dbms_random.string('x', 20) random_string
5 from dual
6 connect by level <= 10;
ID INC_DATETIME RANDOM_ID RANDOM_STRING
---------- ------------------- ---------- --------------------------------------------------------------------------------
1 2009-12-08 19:43:14 76 GWMU280MIVBKKOCZV620
2 2009-12-08 19:43:15 34 GNV88O6TDHD3TWC5GWI5
3 2009-12-08 19:43:16 77 LI6H4O5IAHQIMO4B0WMH
4 2009-12-08 19:43:17 99 LP7XP49I0YOJIYSJDQZO
5 2009-12-08 19:43:18 55 V3284X9RXW4UZI8BQMO3
6 2009-12-08 19:43:19 16 T0OA52UAOGHL1TT46H25
7 2009-12-08 19:43:20 61 UY6RUOF7HWTO86942FLP
8 2009-12-08 19:43:21 25 JYXO4OPEW8J1CKVCPDJR
9 2009-12-08 19:43:22 10 DONU6W9QVQM3KJ2UG8LO
10 2009-12-08 19:43:23 76 J8DJLVNOUIZDXE4UXUJG
10 rows selected
上面SQL是利用了Oracle資料庫文法的幾個實用小技巧實現的:
1、利用Oracle特有的“connect by”樹形串連文法產生測試記錄,“level <= 10”表示要產生10記錄;
2、利用rownum虛擬列產生遞增的整數資料;
3、利用sysdate函數加一些簡單運算來產生日期資料,本例中是每條記錄的時間加1秒;
4、利用dbms_random.value函數產生隨機的數值型資料,本例中是產生0到100之間的隨機整數;
5、利用dbms_random.string函數產生隨機的字元型資料,本例中是產生長度為20的隨機字串,字串中可以包括字元或數字。
ok,那要產生10萬條測試記錄表可以用如下SQL:
create table myTestTable as
select rownum as id,
to_char(sysdate + rownum/24/3600, 'yyyy-mm-dd hh24:mi:ss') as inc_datetime,
trunc(dbms_random.value(0, 100)) as random_id,
dbms_random.string('x', 20) random_string
from dual
connect by level <= 100000;
一、Test Windos方式
declare maxrecords constant int:=1000; i int :=1; begin for i in 1..maxrecords loop insert into test2 (id, name) values (test2_seq.nextval, to_char('9999'+i)); end loop; dbms_output.put_line(' 成功錄入資料! '); commit; end; |
二、從已有表中往入另一張表導資料
create or replace procedure TestProc is begin for c in(select id, name from test2) loop insert into test (id, name) values (test_seq.nextval, c.name); end loop; end TestProc; |