SQL Server How can I populate my database's all tables with random data?

8xiog9wr  于 12个月前  发布在  其他
关注(0)|答案(4)|浏览(109)

Is there such a software out there? It doesn't matter if the data itself make sense. I am just worried about the fields to get populated. Basically, it will read the table definitions and generate some data accordingly. It would also be great if it asks for how many rows to insert per table, whether the default values will be left blank or get populated, how to treat the varchars(to the full extent or up to a given,specified number of characters). Ideally free :) but commercial product suggestions are also highly welcome. Thank you all.

eivnm1vs

eivnm1vs1#

I see there is still no answer that has been chosen as the best. Besides the solutions already mentioned in this thread, I can suggest using ApexSQL Generate – a SQL data generator tool , which has the ability to quickly generate millions of rows, using various data sources, with a variety of data generators.

It features a fully functional free trial, so you can download it and test to see if it will get your job done.

ktecyv1j

ktecyv1j2#

For mysql databases you can use filldb.info .

It automatically generate data and fill your database tables with this dummy data

y53ybaqx

y53ybaqx3#

In case someone is looking for a simple way to do this without extra software.

We just need a function that returns a random number:

-- Create the RANDBETWEEN function
-- Usage: SELECT dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID())))
CREATE FUNCTION dbo.RANDBETWEEN(@minval TINYINT, @maxval TINYINT, @random NUMERIC(18,10))
RETURNS TINYINT
AS
BEGIN
  RETURN (SELECT CAST(((@maxval + 1) - @minval) * @random + @minval AS TINYINT))
END
GO

And then you can run a query like this:

-- Insert 1 million records into the Person table
INSERT INTO Person (FirstName,LastName,CityId)
SELECT TOP 1000000
    CASE
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 0 THEN 'John'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 1 THEN 'Jack'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 2 THEN 'Bill'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 3 THEN 'Mary'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 4 THEN 'Kate'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 5 THEN 'Matt'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 6 THEN 'Rachel'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 7 THEN 'Tom'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 8 THEN 'Ann'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 9 THEN 'Andrew'
    ELSE 'Bob' END AS FirstName,
    CASE
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 0 THEN 'Smith'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 1 THEN 'Morgan'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 2 THEN 'Simpson'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 3 THEN 'Walker'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 4 THEN 'Bauer'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 5 THEN 'Taylor'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 6 THEN 'Morris'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 7 THEN 'Elliot'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 8 THEN 'White'
        WHEN dbo.RANDBETWEEN(0,9,RAND(CHECKSUM(NEWID()))) = 9 THEN 'Davis'
    ELSE 'Brown' END AS LastName,
    dbo.RANDBETWEEN(1,15,RAND(CHECKSUM(NEWID()))) as CityId
FROM sys.all_objects a
CROSS JOIN sys.all_objects b
GO

Result:

Adjust the query accordingly to match your table/column names.

lzfw57am

lzfw57am4#

the very simple SQL I use generally :

create table fillTbl (msg nvarchar(255))
insert into fillTbl select top 4285 description from sys.sysmessages where msglangid=1033 -- ~ 1 Mo
GO 100 -- the number of Mo you want to add
exec sp_spaceused 'fillTbl' -- space in the table
exec sp_spaceused -- space in the database
-- drop table fillTbl -- reset the space added

相关问题