Blog Pages

Generating INSERT statements for a table

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

-- run as script:
declare
@tableName varchar(100),
@schemaName varchar(100) = 'dbo',
 @extraWhere varchar(500)
select @tableName = 'TestTypes'
-- select @extraWhere = ' ID > 12 '

-- run as stored procedure:
/*
CREATE PROCEDURE InsertStatementsGeneratorForTable
@tableName varchar(100),
@schemaName varchar(100) = 'dbo'
as
*/


DECLARE @string nvarchar(max)  --for storing the first half of INSERT statement
DECLARE @stringData nvarchar(max) --for storing the data (VALUES) related statement
DECLARE @dataType nvarchar(max) --data types returned for respective columns
SET @string= N'INSERT INTO ' + @schemaName + '.' + @tableName+ N'('

SET @stringData = ''

DECLARE @colName nvarchar(50)

--Open a cursor to retrieve column specific information for the specified table
DECLARE cursCol CURSOR FAST_FORWARD FOR 
-------------- Start Select --------------------------
SELECT column_name,data_type FROM information_schema.columns 
WHERE table_name = @tableName
AND  TABLE_SCHEMA = @schemaName
-------------- End Select ----------------------------
OPEN cursCol
FETCH NEXT FROM cursCol INTO @colName,@dataType

IF @@fetch_status<>0
 begin
 print N'Table ' + @schemaName + '.' + @tableName+ N' not found, processing skipped.'

 close curscol
 deallocate curscol
 return
END

WHILE @@FETCH_STATUS=0
BEGIN
 IF @dataType in ('varchar','char','nchar','nvarchar')
 BEGIN
  SET @stringData=@stringData+''''+'''+isnull('''''+'''''+REPLACE('+@colName + ', '''''''', '''''''''''')'+'+'''''+''''',''NULL'')+'',''+'
 END
 ELSE
 IF @dataType in ('text','ntext') --if the datatype is text or something else 
 BEGIN
  SET @stringData=@stringData+'''''''''+isnull(cast('+@colName+' as varchar(2000)),'''')+'''''',''+'
 END
 ELSE
 IF @dataType = 'money' --because money doesn't get converted from varchar implicitly
 BEGIN
  SET @stringData=@stringData+'''convert(money,''''''+isnull(cast('+@colName+' as varchar(200)),''0.0000'')+''''''),''+'
 END
 ELSE 
 IF @dataType='datetime'
 BEGIN
  SET @stringData=@stringData+'''convert(datetime,'+'''+isnull('''''+'''''+convert(varchar(200),'+@colName+',121)+'''''+''''',''NULL'')+'',121),''+'
 END
 ELSE 
 IF @dataType='image' 
 BEGIN
  SET @stringData=@stringData+'''''''''+isnull(cast(convert(varbinary,'+@colName+') as varchar(6)),''0'')+'''''',''+'
 END
 ELSE --presuming the data type is int,bit,numeric,decimal 
 BEGIN
  SET @stringData=@stringData+''''+'''+isnull('''''+'''''+convert(varchar(200),'+@colName+')+'''''+''''',''NULL'')+'',''+'
 END

 SET @string=@string+@colName+','

 FETCH NEXT FROM cursCol INTO @colName,@dataType
END

DECLARE @Query nvarchar(max)

SET @query = 'SELECT ''' + 
    substring(@string,0,len(@string)) + 
    ') VALUES(''+ ' + 
    substring(@stringData,0,len(@stringData)-2) +
    '''+'')'' FROM ' + 
    @schemaName + '.' + 
    @tableName
IF @extraWhere IS NOT NULL

                SET @query = @query + N' WHERE ' + @extraWhere

exec sp_executesql @query
--select @query

print @stringData
print @string
print @query

CLOSE cursCol
DEALLOCATE cursCol

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

The source code was taken from the link below, and edited by me.
http://www.codeproject.com/KB/database/InsertGeneratorPack.aspx

Another option - script via SSMS:
Right click on the DB name--> Tasks --> Generate Scripts --> in the advanced settings:
Displaying image001.png

No comments:

Post a Comment