Script to generate testing database tables and dummy records

Who is online?  0 guests and 0 members
Home  »  Articles  »  Script to generate testing database tables and dummy records

Script to generate testing database tables and dummy records

change text size: A A A
Published: 7/13/2011 by  NayanPatel  - Views:  [666]  

Some times we have need to create a sample database for testing purpose and we need ability to define how many tables, columns and rows you want to generate for each database.... well here is a very simple script which will literally spam your database with hundreds of objects in few minutes :) 

/*
All Copy Rights Reserved : Pragmatic Works
Data Object generation Script V1.0

Create Database DTSX_31
GO
Use DTSX_31
Go
*/
set nocount on 

declare @howmanytables int
declare @howmanycolumns int
declare @howmanyrows int

set @howmanytables=1  -- Total tables to generate
set @howmanycolumns=350 -- Total columns per table to generate
set @howmanyrows = 500 -- Total rows per table
-----------------------------

declare @createprocs bit -- create stored proc for each table 
declare @createviews bit -- create view for each table 

set @createprocs=0
set @createviews=0
-------------------------------
declare @cnt1 int
declare @cnt2 int
declare @cnt3 int

declare @tblname varchar(100)

declare @cols varchar(max)
declare @collist varchar(max)
declare @valuelist varchar(max)
declare @nl char(2)


set @nl=char(13) + char(10)

declare @colname varchar(100)
declare @sql varchar(max)

set @cnt1=1

--//Tables-----------------------------
while @cnt1<=@howmanytables
begin
	set @cols=''
	set @cnt2=1
	 declare @insertCols varchar(max)
	 declare @insertVals varchar(max)
	 select @insertCols='',@insertVals=''
	 --//Columns-----------------------------
	 while @cnt2<=@howmanycolumns
	 begin
		set @colname='col_' + cast(@cnt2 as varchar(10))
		if @cnt2=1
		begin
			set @insertCols=@colname
			set @insertVals='''{row}'''
			set @cols=@colname + ' int not null primary key'
		end
		else
		begin
			set @insertCols=@insertCols + ',' + @colname
			set @insertVals=@insertVals + ',''R{row}-' +  @colname + ''''
			set @cols=@cols + ',' + @colname + ' varchar(100) null'
		end
		set @cnt2=@cnt2+1
	 end
 
	set @tblname= 'table_' + cast(@cnt1 as varchar(10))
	set @sql='create table ' + @tblname +  '(' + @cols  + ')'+ @nl	
	print @sql
	execute (@sql)

	--//Rows-----------------------------
	declare @datascript varchar(max)
	set @datascript=''
	set @cnt3=1
	while @cnt3<=@howmanyrows
	begin
		set @datascript = @datascript + 'insert into ' + @tblname + ' (' + @insertCols + ') values (' + replace(@insertVals,'{row}',cast(@cnt3 as varchar(20))) + ') ' + char(13) + char(10) 
		if (@cnt3 % 100) = 0
		begin
			--//Insert every 100 Rows
			exec(@datascript)
			set @datascript=''
			print 'create total ' + cast(@cnt3 as varchar(100)) +  ' rows'
		end
		set @cnt3=@cnt3+1
	end
	print 'create total ' + cast(@cnt3 as varchar(100)) +  ' rows'
	exec(@datascript)
	
	--//views-----------------------------
	if @createviews=1
	begin
		set @sql='create view vw_' + @tblname +  @nl + ' as ' + @nl + ' select * from ' + @tblname + @nl	
		print @sql
		execute (@sql)
	end
	
	--//procs-----------------------------
	if @createprocs=1
	begin
		set @sql='create proc usp_' + @tblname +  @nl + ' as ' + @nl + ' select * from ' + @tblname + @nl	
		print @sql
		execute (@sql)
	end
	set @cnt1=@cnt1+1
end


 

dafuni likes this.
 
0
/5
Avg: 0/5: (0 votes)

Comments (4)

briankmcdonald

Thanks for sharing this Nayan. Sometimes it is good to just have junk data to play with and this script will definitely help with that.

7/13/2011
 · 
 
by
NayanPatel
NayanPatel said:

You are welcome :) ...

7/13/2011
 · 
 
by
smartkhilit
smartkhilit said:

hi nayan, script is nice & quick solution.

just one tweak... in order to get real-time kind of data/values, we can use Rand() or Floor(Rand()) or something..

Thanks for sharing :)

7/14/2011
 · 
 
by
NayanPatel
NayanPatel said:

Yep I am going to add random NULLs and Random data in next version...

7/14/2011
 · 
 
by

Most Recent Articles