Twitter

Follow SQLQuill on Twitter

Subscribe

MN Pass

Script for Generating Random Data

I often need to fill up tables with random data to test data conversion scripts, foreign keys, indexes, etc. Here’s my “Go To” script for coming up with random data. I usually base it off this and change the name of the table to insert data into, but this has the things I need to remember, like an easy way to loop through code, NEWID(), RAND(), RND(), etc.

Declare @TestTable Table
	(ID uniqueidentifier,
	Num int,
	InsertDate datetime)
Declare @icount int,
	@TopNum int,
	@Repeat int
Set @icount = 1

-------------------------
---Set the ranges here---
-------------------------
Set @TopNum = 10 --Highest number you want - note this will be 0 - 10
Set @Repeat = 1000 --number of times to repeat
-------------------------
-------------------------

While @icount < @Repeat
begin
	insert @TestTable
	Select NewID(),
			ROUND((RAND() * (@TopNum)),0),
			GETDATE()
	Set @icount = @icount + 1
End

Select Num, count(Num) from @TestTable group by Num
order by Num

4 comments to Script for Generating Random Data

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>