Skip to content

Instantly share code, notes, and snippets.

@FrankDeGroot
Last active December 23, 2015 19:39
Show Gist options
  • Save FrankDeGroot/6683783 to your computer and use it in GitHub Desktop.
Save FrankDeGroot/6683783 to your computer and use it in GitHub Desktop.
Create insert statements for a table for SQL Server 2005+.
/*
use tempdb
if exists(select null from sys.objects where name = 'a') drop table a
create table a (bt bit, ti tinyint, si smallint, i int, bi bigint, n numeric, de decimal, sm smallmoney, m money, f float, r real, dt datetime, sdt smalldatetime, da date, tm time, dto datetimeoffset, c char, vc varchar, te text, nc nchar, nvc nvarchar, nt ntext, im image, u uniqueidentifier, x xml, bn binary, vb varbinary)
insert a values(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, '1-1-1', '1:1:1', '1-1-1', '''', '''', '''', '''', '''', '''', '''', '11111111-1111-1111-1111-111111111111', '<x x="''"/>', 1, 1)
insert a values(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null)
exec insertgenerator @Table = 'a', @uppercase = 1
exec insertgenerator @Table = 'dbo.a', @uppercase = 1
drop table a
*/
if object_id('inge', 'P') is not null drop proc inge
go
create proc inge
@Table nvarchar(max),
@Schema nvarchar(max) = 'dbo',
@Where nvarchar(max) = '1=1',
@Uppercase bit = 0
as
declare
@ConvertValue nvarchar(max),
@Name nvarchar(max),
@Names nvarchar(max),
@Query nvarchar(max),
@QuoteValue nvarchar(max),
@Values nvarchar(max);
select
@Name = cast(column_name as nvarchar(max)),
-- List of column names separated by ', '.
@Names = coalesce(@Names + ', ', '') + @Name,
-- Create expression to convert value to varchar.
@ConvertValue =
case
when data_type in ('bit', 'tinyint', 'smallint', 'int', 'bigint', 'numeric', 'decimal', 'money', 'smallmoney', 'float', 'real', 'char', 'varchar', 'text', 'nchar', 'nvarchar', 'ntext', 'uniqueidentifier', 'xml')
then 'cast(' + @Name + ' as nvarchar(max)) collate database_default'
when data_type in ('binary', 'varbinary') -- SQL 2008 Only.
then 'convert(nvarchar(max), ' + @Name + ', 1)'
when data_type in ('datetime', 'smalldatetime', 'date', 'time', 'datetimeoffset', 'datetime2')
then 'convert(nvarchar(max), ' + @Name + ', 121)'
when data_type = 'image'
then 'cast(cast(' + @Name + ' as varbinary) as nvarchar(max))'
else @Name
end,
-- Create expression to quote value if required.
@QuoteValue =
case
when data_type in ('datetime', 'smalldatetime', 'date', 'time', 'datetime2', 'datetimeoffset', 'image', 'varchar', 'char', 'nchar', 'nvarchar', 'text', 'ntext', 'uniqueidentifier', 'xml')
then replicate('''', 4) + ' + replace(' + @ConvertValue + ', ' + replicate('''', 4) + ', ' + replicate('''', 6) + ') + ' + replicate('''', 4)
else @ConvertValue
end,
-- Expression to list column values separated by ', '.
@Values = coalesce(@Values + ' + '', '' + ', '') + 'isnull(' + @QuoteValue + ', ''null'')',
@Query = 'select ''' +
case @Uppercase
when 1 then 'INSERT INTO'
else 'insert into'
end + ' ' + table_schema + '.' + table_name + ' (' + @Names + ') ' +
case @Uppercase
when 1 then 'VALUES'
else 'values'
end + ' ('' + ' + @Values +
' + '');'' from ' + table_schema + '.' + table_name +
' where ' + @Where
from information_schema.columns
where
table_schema = @Schema and
table_name = @Table or
(
table_schema + '.' + table_name = @Table
)
order by ordinal_position;
exec sp_executesql @Query;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment