SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to take script for database table data


how to take script for database table data

Author
Message
homebrew01
homebrew01
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12886 Visits: 9222
Here is a script for generating INSERT statements for 1 table. You could modify it for all tables or a subset. I use it when generating a subset of data for testing. I think someone here on this forum provided it .... Sorry, I did not note who it was (although I usually do try to keep the authors name).

Edit: Maybe I got it from here:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=53420

-- Instrucions!
-- Change current db
-- Change source table (in red)
-- Change sample rows (in red)
-- run to generate insert statements
-- optional 'WHERE' clause near the bottom to specify records
use MyDatabase

DECLARE @tableName varchar(100),
@seedCnt int

SET @tableName = 'MyTable' -- Table Name
SET @seedCnt = 50 -- # of records to select

DECLARE @execStr0 varchar(8000),
@execStr1 varchar(8000),
@execStr2 varchar(8000),
@execStr3 varchar(8000),
@execStr4 varchar(8000),
@execStr5 varchar(8000),
@execStr6 varchar(8000),
@execStr7 varchar(8000),
@execStr8 varchar(8000),
@execStr9 varchar(8000)

-- Display warning for unsupported types
IF EXISTS(SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @tablename
AND DATA_TYPE NOT IN
('uniqueidentifier', 'char', 'nchar', 'varchar', 'nvarchar', 'sysname',
'datetime', 'tinyint', 'int', 'float', 'numeric', 'decimal', 'smallmoney',
'money', 'bit', 'smallint', 'real', 'bigint'))
BEGIN
SELECT DISTINCT DATA_TYPE + ' Type not supported'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @tablename
AND DATA_TYPE NOT IN
('uniqueidentifier', 'char', 'nchar', 'varchar', 'nvarchar', 'sysname',
'datetime', 'tinyint', 'int', 'float', 'numeric', 'decimal', 'smallmoney',
'money', 'bit', 'smallint', 'real', 'bigint')
END
-- Build column translations
SELECT
@execStr1 = CASE WHEN LEN(@execStr8) > 7500 THEN @execStr2 ELSE @execStr1 END,
@execStr2 = CASE WHEN LEN(@execStr8) > 7500 THEN @execStr3 ELSE @execStr2 END,
@execStr3 = CASE WHEN LEN(@execStr8) > 7500 THEN @execStr4 ELSE @execStr3 END,
@execStr4 = CASE WHEN LEN(@execStr8) > 7500 THEN @execStr5 ELSE @execStr4 END,
@execStr5 = CASE WHEN LEN(@execStr8) > 7500 THEN @execStr6 ELSE @execStr5 END,
@execStr6 = CASE WHEN LEN(@execStr8) > 7500 THEN @execStr7 ELSE @execStr6 END,
@execStr7 = CASE WHEN LEN(@execStr8) > 7500 THEN @execStr8 ELSE @execStr7 END,
@execStr8 = CASE WHEN LEN(@execStr8) > 7500 THEN NULL ELSE @execStr8 END,

@execStr8 = IsNull(@execStr8 + ' +' + CHAR(13) + CHAR(10),'') +
CONVERT(varchar(8000),
CASE
WHEN DATA_TYPE IN ('uniqueidentifier')
THEN CHAR(9) + '''' + COLUMN_NAME
+ '=''+IsNull('''''''' + CONVERT(varchar(50),' + COLUMN_NAME + ') + '''''''',''null'')+'', '''
WHEN DATA_TYPE IN ('char', 'nchar', 'varchar', 'nvarchar', 'sysname')
THEN CHAR(9) + '''' + COLUMN_NAME
+ '=''+IsNull('''''''' + REPLACE(' + COLUMN_NAME + ','''''''','''''''''''') + '''''''',''null'')+'', '''
WHEN DATA_TYPE IN ('datetime')
THEN CHAR(9) + '''' + COLUMN_NAME
+ '=''+IsNull('''''''' + CONVERT(varchar,' + COLUMN_NAME + ',121)+'''''''',''null'') + '', '''
WHEN DATA_TYPE IN ('tinyint', 'int', 'float', 'numeric', 'decimal', 'smallmoney', 'money', 'bit', 'smallint', 'real', 'bigint')
THEN CHAR(9) + '''' + COLUMN_NAME
+ '=''+IsNull(CONVERT(varchar,' + COLUMN_NAME + '),''null'')+'', '''
ELSE
' ** DATA TYPE ' + DATA_TYPE + ' NOT SUPPORTED **'
END)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @tablename
AND DATA_TYPE IN
('uniqueidentifier', 'char', 'nchar', 'varchar', 'nvarchar', 'sysname',
'datetime', 'tinyint', 'int', 'float', 'numeric', 'decimal', 'smallmoney',
'money', 'bit', 'smallint', 'real', 'bigint')
ORDER BY ORDINAL_POSITION

SELECT @execStr0 = 'Select top ' + CONVERT(varchar, @seedCnt) + CHAR(13) + CHAR(10)
+ '''Insert Into #' + REPLACE(@tableName,' ','') + ' Select '' + '
+ CHAR(13) + CHAR(10)
SELECT @execStr1 = IsNull(@execStr1+', ', '')
SELECT @execStr2 = IsNull(@execStr2+', ', '')
SELECT @execStr3 = IsNull(@execStr3+', ', '')
SELECT @execStr4 = IsNull(@execStr4+', ', '')
SELECT @execStr5 = IsNull(@execStr5+', ', '')
SELECT @execStr6 = IsNull(@execStr6+', ', '')
SELECT @execStr7 = IsNull(@execStr7+', ', '')
SELECT @execStr8 = left(@execStr8, len(@execStr8)-4) + ''''''
SELECT @execStr9 = CHAR(13) + CHAR(10) + 'from [' + @tableName + ']' --+ 'where Answer_ID < 20'

-- Comment in for Debug
-- Select @execStr0, @execStr1, @execStr2, @execStr3, @execStr4, @execStr5, @execStr6, @execStr7, @execStr8, @execStr9

EXEC (@execStr0 + @execStr1 + @execStr2 + @execStr3 + @execStr4
+ @execStr5 + @execStr6 + @execStr7 + @execStr8 + @execStr9)





ErikV
ErikV
Valued Member
Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)

Group: General Forum Members
Points: 64 Visits: 27
I use www.sqlserverbooster.com

This software generates complete script backup, full, differential and log; as you wish. Supports since 2000 MS SQL Server versions to the present. Also let you generates your script in batch files, because if it's too big at the moment to generate the database again it is little tricky to restore with a huge script.

Besides let you upload the backups to the cloud, schedule the backups an more.

The good thing is that it's FREE.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search