Auto populate table with SQL date range

My problem is the auto-complete of the table. I have a table with 1000 records, but for testing purposes I need to insert more data.

ID | Personal ID | Date | Time | Amount | TypeID | PlaceID | StatusID

So I need to populate the database with 10,000 records, where the date is between 1/3/2015 and 1/5/2015, Time is Random, SUM Between 100 and 1000, TypeID between 1 and 2, PlaceID between 1-10, StatusID between 1-3

I would appreciate any help or suggestion.

Thanks in advance.

+3


source to share


3 answers


This requires a small 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;

      



Also, do not try to use column names such as "ID", "Date", "Time", etc., which have special meanings in SQL Server.

+2


source


Here's some brutal solution, but completely randomized:

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

      



Fiddle http://sqlfiddle.com/#!6/9eecb7db59d16c80417c72d1/892

+3


source


One is to use pseudo-random values ​​obtained from NEWID. You haven't specified how to assign the ID and PersonID, but the ROW_NUMBER value returned by the CTE can be used to do this if you need incremental values.

WITH 
    t4 AS (SELECT n FROM (VALUES(0),(0),(0),(0)) t(n))
    ,t256 AS (SELECT 0 AS n FROM t4 AS a CROSS JOIN t4 AS b CROSS JOIN t4 AS c CROSS JOIN t4 AS d)
    ,t16M AS (SELECT ROW_NUMBER() OVER (ORDER BY (a.n)) AS num FROM t256 AS a CROSS JOIN t256 AS b CROSS JOIN t256 AS c)
SELECT 
     DATEADD(day, CAST(CAST(NEWID() AS varbinary(1)) AS int) % 3, '20150103') AS Date
    ,DATEADD(millisecond, CAST(CAST(NEWID() AS varbinary(4)) AS int), CAST('' AS time)) AS Time
    ,(CAST(CAST(NEWID() AS varbinary(3)) AS int) % 900) + 100 AS [Sum]
    ,(CAST(CAST(NEWID() AS varbinary(3)) AS int) % 2) + 1 AS TypeID
    ,(CAST(CAST(NEWID() AS varbinary(3)) AS int) % 10) + 1 AS PlaceID
    ,(CAST(CAST(NEWID() AS varbinary(3)) AS int) % 3) + 1 AS StatisID
FROM t16M
WHERE num <= 10000;

      

+2


source







All Articles