我的问题是自动填充表格。 我的表中有1000条记录,但出于测试目的,我需要插入更多数据。
ID | PersonID |日期|时间|总和| TypeID | PlaceID | StatusID
所以我需要用10000条记录填充数据库,其中日期介于2015年1月1日到2015年5月5日之间,时间是随机的,SUM介于100和1000之间,TypeID介于1和2之间,PlaceID介于1之间10,StatusID在1-3之间
我会感谢任何帮助或建议。
提前致谢。
答案 0 :(得分:3)
这是一些残酷的解决方案,但完全随机化:
with rows as(select row_number() over(order by(select null)) as dummy from
(values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1))t1(n)
cross join (values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1))t2(n)
cross join (values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1))t3(n)
cross join (values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1))t4(n))
select *,
cast(dateadd(ms, cast(cast(newid() as varbinary(30)) as int), getdate()) as time) as time
from rows r
cross apply(select top 1 p as place
from (values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10))p(p)
where r.dummy = r.dummy order by newid()) cap
cross apply(select top 1 s as status
from (values(1),(2),(3))s(s)
where r.dummy = r.dummy order by newid()) cas
cross apply(select top 1 t as time
from (values(1),(2))t(t)
where r.dummy = r.dummy order by newid()) cat
cross apply(select top 1 sum from(select 100 + row_number() over(order by(select null)) as sum
from (values(1),(1),(1),(1),(1),(1),(1),(1),(1))t1(n)
cross join (values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1))t2(n)
cross join (values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1))t3(n)) t
where r.dummy = r.dummy order by newid()) casu
cross apply(select top 1 dateadd(dd, s -1, '20150103') as date
from (values(1),(2),(3))s(s)
where r.dummy = r.dummy order by newid()) cad
答案 1 :(得分:2)
你需要一个小的t-sql来完成它:
--CREATE TABLE TEST (CID INT, PERSONID INT, TEST_DATE DATE, TEST_TIME TIME, TEST_SUM INT, TYPEID INT, PLACEID INT, STATUSID INT);
--TRUNCATE TABLE TEST;
SET NOCOUNT ON;
DECLARE @X INT, @PERSONID INT, @DATE DATE, @TIME TIME, @SUM INT, @TYPEID INT, @PLACEID INT, @STATUSID INT,@R INT;
SELECT @X=0;
WHILE @X < 10000 BEGIN
SELECT @X=@X +1;
SELECT @DATE = DATEADD(DAY, @X / 4000, '2015-1-3');
SELECT @R=CONVERT(INT, RAND() * 3600 * 24);
SELECT @TIME = DATEADD(SECOND, @R , '00:00:01');
SELECT @SUM = 100 + @R % 900;
SELECT @TYPEID = @R % 2 + 1 ;
SELECT @PLACEID = @R % 10 +1 ;
SELECT @STATUSID = @R % 3 +1 ;
SELECT @PERSONID = @R % 500 +1 ;
INSERT INTO TEST (CID, PERSONID, TEST_DATE, TEST_TIME, TEST_SUM, TYPEID, PLACEID, STATUSID)
VALUES(@X, @PERSONID, @DATE, @TIME, @SUM, @TYPEID, @PLACEID, @STATUSID);
END;
SET NOCOUNT OFF;
另外,请尽量不要使用SQL Server中具有特殊含义的“ID”,“日期”,“时间”等列名。
答案 2 :(得分:2)
一种是使用从NEWID派生的伪随机值。您没有提及如何分配ID和PersonID,但如果您需要增量值,CTE返回的ROW_NUMBER值可用于此。
ExecutorService threadPool = Executors.newFixedThreadPool(10);
for (int i = 0; i < X; i++) {
threadPool.submit(new Runnable() {
public void run() {
Function_to_run(i);
}
});
}