Home > Back-end >  How to generate ten million test/sample records for SQL Server Address Table (AddressID, FirstLine,
How to generate ten million test/sample records for SQL Server Address Table (AddressID, FirstLine,

Time:07-19

Database: SQL Server 2019

Table name: Address

Columns: 
AddressID Varchar(20)
FirstLine Varchar(400)
SecondLine Varchar(400)
State      Varchar (100)
Country    Varchar (50)

Sample Data:

AddrID0001 | Some Random Street | Some Random Apt | NH | US 
AddrID0002 | ueiwoqtyr uiyweqry qow iuyuiwqye | ewquyrtweq Apt 4| CA | US 
AddrID0003 | rtyewqr yuwqtert  oiyqewiru  | ewquyrtweq utyewqr | NC| US 
etc.

If these random placeholders can be replaced by actual names, that will be great but not necessary.

CodePudding user response:

Create a table with a few records.For example, 10 records. Then fill the destination table with the following code and writing the desired number in front of the GO command

INSERT INTO Address
VALUES (
    (SELECT TOP 1 FirstLine FROM tbl ORDER BY NEWID()) ,
    (SELECT TOP 1 SecondLine FROM tbl ORDER BY NEWID()) ,
    (SELECT TOP 1 State FROM tbl ORDER BY NEWID()) ,
    (SELECT TOP 1 Country FROM tbl ORDER BY NEWID())
)
GO 10000000

With this GO 10000000 command, INSERT queries will be executed 10000000 times

CodePudding user response:

WITH  cte as (
   select CAST(0 AS INT) as x
   union all
   select x 1 from cte where x<9)
INSERT INTO Address
SELECT
  CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(CONCAT('AddrID0' , cte1.x),  cte2.x) , cte3.x) , cte4.x) , cte5.x) , cte6.x) , cte7.x) as AddressID,
  REPLACE(REPLACE(CONVERT(varchar(255), NEWID()),'-',' '),'Q',' ') as FirstLine,
  REPLACE(REPLACE(CONVERT(varchar(255), NEWID()),'-',' '),'Q',' ') as SecondLine,
  'NH',
  'US'
FROM cte as cte1
CROSS JOIN cte as cte2
CROSS JOIN cte as cte3
CROSS JOIN cte as cte4
CROSS JOIN cte as cte5
CROSS JOIN cte as cte6
CROSS JOIN cte as cte7
-- ORDER BY 1
;

output of this query:

select * from (
(select * from (select top(3) * from Address  ORDER by AddressID asc) x1 )
union all 
(select * from (select top(3) * from Address order by AddressID desc)  x2)
) y;

will be something (because of the random output of NEWID()) like:

AddressID FirstLine SecondLine State Country
AddrID00000000 21348E1C D239 4DC5 AA28 3D609919C9C4 AD9C16F1 9B9D 49F9 A249 7893C7EBEDBC NH US
AddrID00000001 D9BDAF89 9147 4F8E BBCB 0345411B7DD8 79C50237 2181 45A2 8551 2A574DD71145 NH US
AddrID00000002 83818A03 A341 4ED7 AEB8 001CC4162276 9301BDF4 F456 484F BE8C DCDD7EB44060 NH US
AddrID09999999 D22C6EBB DC33 4572 96F4 8C24C350BEB0 21C946B6 0ECB 46A7 99A0 569B43C3275C NH US
AddrID09999998 A52E42DD 7BBA 4440 89D3 825176ED0159 CCBD542F 28E9 449D 8A4C 9AA5ED4E9923 NH US
AddrID09999997 02F640F6 786E 43D4 95F6 F80A4AA10CA3 88AB6376 65F7 4B5B 9C9C 78D67822B1CD NH US
  • It's up to you to fill State with something random.
  • "quickly" is a relative word, on my system it took about 2 minutes.
  • The REPLCE in First- and SecondLine is just a simple attempt to create random length words, and is open for improvement when you have more imagination than I have currently...
  • Related