Wednesday, June 22, 2016

Create few tables & insert some data...

--For testing purpose, sometimes we may need to create some tables & insert some data into the tables.
--Here is a dynamic script to create a number of tables & insert lots of data.
--Author: Mustak Ahammad Prince
--Purpose: Create few tables & insert some data...

USE Loan
GO

DECLARE @tableStr varchar(max)
DECLARE @insertStr varchar(max)
DECLARE @tableStr1 varchar(max) = 'CREATE TABLE '
DECLARE @tableStr2 varchar(max) = ' (id int, value1 varchar(max),value2 varchar(max))'
DECLARE @insertStr1 varchar(max) = 'INSERT INTO '
DECLARE @insertStr2 varchar(max) = ' VALUES ( '
DECLARE @insertStr3 varchar(max) = ', ''ABC'',''DEF'')'
DECLARE @tableName varchar(20) = 'dba_'
DECLARE @start int = 1

while (@start<=20)
BEGIN
DECLARE @newName varchar(max)= @tableName + cast(@start as varchar(max))
--print @newName + char(13)
SET @start = @start +1
SET @tableStr = @tableStr1 + @newName +@tableStr2
EXEC (@tableStr)
--print char (13)+@tableStr+char(13)

--print char(13)+ @insertStr1 + char(13)
DECLARE @newP int = 1
while (@newP<100)
BEGIN
SET @insertStr = @insertStr1 + @newName + @insertStr2 + cast(@newP as varchar(max) )+ @insertStr3
EXEC (@insertStr)
--print(@insertStr)
SET @newP = @newP +1
END
END

No comments:

Post a Comment