ssms - MS SQL server "Generate script" with insert values in a single command -
when using "generate script" feature in ms sql management studio data exported well, values inserted in separate steps, looks
insert [dbo].[table] ([table_id], [table_version], [table_timestamp], [table_user], [table_percent], [table_hid], [table_latestversionid], [table_isdeleted]) values (3, -1, cast(n'2015-06-04 13:03:38.000' datetime), n'init', n' 30 %', null, 13, 0) insert [dbo].[table] ([table_id], [table_version], [table_timestamp], [table_user], [table_percent], [table_hid], [table_latestversionid], [table_isdeleted]) values (4, -1, cast(n'2015-06-04 13:03:38.000' datetime), n'init', n' 40 %', null, 14, 0) insert [dbo].[table] ([table_id], [table_version], [table_timestamp], [table_user], [table_percent], [table_hid], [table_latestversionid], [table_isdeleted]) values (5, -1, cast(n'2015-06-04 13:03:38.000' datetime), n'init', n' 50 %', null, 15, 0) insert [dbo].[table] ([table_id], [table_version], [table_timestamp], [table_user], [table_percent], [table_hid], [table_latestversionid], [table_isdeleted]) values (6, -1, cast(n'2015-06-04 13:03:38.000' datetime), n'init', n' 60 %', null, 16, 0) insert [dbo].[table] ([table_id], [table_version], [table_timestamp], [table_user], [table_percent], [table_hid], [table_latestversionid], [table_isdeleted]) values (7, -1, cast(n'2015-06-04 13:03:38.000' datetime), n'init', n' 70 %', null, 17, 0) insert [dbo].[table] ([table_id], [table_version], [table_timestamp], [table_user], [table_percent], [table_hid], [table_latestversionid], [table_isdeleted]) values (8, -1, cast(n'2015-06-04 13:03:38.000' datetime), n'init', n' 80 %', null, 18, 0)
is there way make script generation , forcing ssms create insert queries in single or several larger steps, like:
insert [dbo].[table] ([table_id], [table_version], [table_timestamp], [table_user], [table_percent], [table_hid], [table_latestversionid], [table_isdeleted]) values (3, -1, cast(n'2015-06-04 13:03:38.000' datetime), n'init', n' 30 %', null, 13, 0), (4, -1, cast(n'2015-06-04 13:03:38.000' datetime), n'init', n' 40 %', null, 14, 0), (5, -1, cast(n'2015-06-04 13:03:38.000' datetime), n'init', n' 50 %', null, 15, 0), (6, -1, cast(n'2015-06-04 13:03:38.000' datetime), n'init', n' 60 %', null, 16, 0), (7, -1, cast(n'2015-06-04 13:03:38.000' datetime), n'init', n' 70 %', null, 17, 0), (8, -1, cast(n'2015-06-04 13:03:38.000' datetime), n'init', n' 80 %', null, 18, 0);
thanks
--first create sp generateinsert run below mention code give desire output.. if exists (select 1 sys.objects object_id = object_id(n'dbo.generateinsert') , type in (n'p', n'pc')) drop procedure dbo.generateinsert; go create procedure dbo.generateinsert ( @objectname nvarchar(261) , @targetobjectname nvarchar(261) = null , @ommitinsertcolumnlist bit = 0 , @generatesingleinsertperrow bit = 0 , @useselectsyntax bit = 0 , @usecolumnaliasinselect bit = 0 , @formatcode bit = 1 , @generateonecolumnperline bit = 0 , @generatego bit = 0 , @printgeneratedcode bit = 1 , @topexpression nvarchar(max) = null , @functionparameters nvarchar(max) = null , @searchcondition nvarchar(max) = null , @orderbyexpression nvarchar(max) = null , @ommitunsupporteddatatypes bit = 1 , @populateidentitycolumn bit = 0 , @populatetimestampcolumn bit = 0 , @populatecomputedcolumn bit = 0 , @generateprojectinfo bit = 1 , @generatesetnocount bit = 1 , @generatestatementterminator bit = 1 , @showwarnings bit = 1 , @debug bit = 0 ) begin set nocount on; declare @crlf char(2) set @crlf = char(13) + char(10); declare @columnname sysname; declare @datatype sysname; declare @columnlist nvarchar(max); set @columnlist = n''; declare @selectlist nvarchar(max); set @selectlist = n''; declare @selectstatement nvarchar(max); set @selectstatement = n''; declare @omittedcolumnlist nvarchar(max); set @omittedcolumnlist = n''; declare @insertsql nvarchar(max); set @insertsql = n'insert ' + coalesce(@targetobjectname,@objectname); declare @valuessql nvarchar(max); set @valuessql = n'values ('; declare @selectsql nvarchar(max); set @selectsql = n'select '; declare @tabledata table (tablerow nvarchar(max)); declare @results table (tablerow nvarchar(max)); declare @tablerow nvarchar(max); declare @rowno int; if parsename(@objectname,3) not null or parsename(@objectname,4) not null begin raiserror(n'server , database names not allowed specify in @objectname parameter. required format [schema_name.]object_name',16,1); return -1; end if object_id(@objectname,n'u') null -- user_table , object_id(@objectname,n'v') null -- view , object_id(@objectname,n'if') null -- sql_inline_table_valued_function , object_id(@objectname,n'tf') null -- sql_table_valued_function begin raiserror(n'user table, view, table-valued or inline function %s not found or insuficient permission query provided object.',16,1,@objectname); return -1; end if not exists ( select 1 information_schema.tables table_type in ('base table','view') , table_name = parsename(@objectname,1) , (table_schema = parsename(@objectname,2) or parsename(@objectname,2) null) ) , not exists ( select * information_schema.routines routine_type in ('function') , data_type = 'table' , specific_name = parsename(@objectname,1) , (specific_schema = parsename(@objectname,2) or parsename(@objectname,2) null) ) begin raiserror(n'user table, view, table-valued or inline function %s not found or insuficient permission query provided object.',16,1,@objectname); return -1; end declare columncursor cursor local fast_forward select c.name columnname ,coalesce(type_name(c.system_type_id),t.name) datatype sys.objects o inner join sys.columns c on c.object_id = o.object_id left join sys.types t on t.system_type_id = c.system_type_id , t.user_type_id = c.user_type_id o.type in (n'u',n'v',n'if',n'tf') -- u = user_table -- v = view -- if = sql_inline_table_valued_function -- tf = sql_table_valued_function , (o.object_id = object_id(@objectname) or o.name = @objectname) , (columnproperty(c.object_id,c.name,'isidentity') != 1 or @populateidentitycolumn = 1) , (columnproperty(c.object_id,c.name,'iscomputed') != 1 or @populatecomputedcolumn = 1) order columnproperty(c.object_id,c.name,'ordinal') -- ordinal_position read ; open columncursor; fetch next columncursor @columnname,@datatype; while @@fetch_status = 0 begin -- handle different data types declare @columnexpression nvarchar(max); set @columnexpression = case when @datatype in ('char','varchar','text','uniqueidentifier') n'isnull(''''''''+replace(convert(varchar(max),'+ quotename(@columnname) + n'),'''''''','''''''''''')+'''''''',''null'') collate database_default' when @datatype in ('nchar','nvarchar','sysname','ntext','sql_variant','xml') n'isnull(''n''''''+replace(convert(nvarchar(max),'+ quotename(@columnname) + n'),'''''''','''''''''''')+'''''''',''null'') collate database_default' when @datatype in ('int','bigint','smallint','tinyint','decimal','numeric','bit') n'isnull(convert(varchar(max),'+ quotename(@columnname) + n'),''null'') collate database_default' when @datatype in ('float','real','money','smallmoney') n'isnull(convert(varchar(max),'+ quotename(@columnname) + n',2),''null'') collate database_default' when @datatype in ('datetime','smalldatetime','date','time','datetime2','datetimeoffset') n'''convert('+@datatype+',''+isnull(''''''''+convert(varchar(max),'+ quotename(@columnname) + n',121)+'''''''',''null'') collate database_default' + '+'',121)''' when @datatype in ('rowversion','timestamp') case when @populatetimestampcolumn = 1 n'''convert(varbinary(max),''+isnull(''''''''+convert(varchar(max),convert(varbinary(max),'+ quotename(@columnname) + n'),1)+'''''''',''null'') collate database_default' + '+'',1)''' else n'''null''' end when @datatype in ('binary','varbinary','image') n'''convert(varbinary(max),''+isnull(''''''''+convert(varchar(max),convert(varbinary(max),'+ quotename(@columnname) + n'),1)+'''''''',''null'') collate database_default' + '+'',1)''' when @datatype in ('geography') -- convert geography text: ?? column.stastext(); -- convert text geography: ?? geography::stgeomfromtext('linestring(-122.360 47.656, -122.343 47.656 )', 4326); null else null end; if @columnexpression null , @ommitunsupporteddatatypes != 1 begin raiserror(n'datatype %s not supported. use @ommitunsupporteddatatypes exclude unsupported columns.',16,1,@datatype); return -1; end if @columnexpression null begin set @omittedcolumnlist = @omittedcolumnlist + case when @omittedcolumnlist != n'' n'; ' else n'' end + n'column ' + quotename(@columnname) + n', datatype ' + @datatype; end if @columnexpression not null begin set @columnlist = @columnlist + case when @columnlist != n'' n',' else n'' end + quotename(@columnname) + case when @generateonecolumnperline = 1 @crlf else n'' end; set @selectlist = @selectlist + case when @selectlist != n'' n'+'',''+' + @crlf else n'' end + @columnexpression + case when @usecolumnaliasinselect = 1 , @useselectsyntax = 1 n'+'' ' + quotename(@columnname) + n'''' else n'' end + case when @generateonecolumnperline = 1 n'+char(13)+char(10)' else n'' end; end fetch next columncursor @columnname,@datatype; end close columncursor; deallocate columncursor; if nullif(@columnlist,n'') null begin raiserror(n'no columns select.',16,1); return -1; end if @debug = 1 begin print(n'--column list'); print(@columnlist); end if nullif(@omittedcolumnlist,'') not null , @showwarnings = 1 begin print(n'--*************************'); print(n'--warning: following columns have been omitted because of unsupported datatypes: ' + @omittedcolumnlist); print(n'--*************************'); end if @generatesingleinsertperrow = 1 begin set @selectlist = n'''' + @insertsql + n'''+' + @crlf + case when @formatcode = 1 n'char(13)+char(10)+' + @crlf else n''' ''+' end + case when @ommitinsertcolumnlist = 1 n'' else n'''(' + @columnlist + n')''+' + @crlf end + case when @formatcode = 1 n'char(13)+char(10)+' + @crlf else n''' ''+' end + case when @useselectsyntax = 1 n'''' + @selectsql + n'''+' else n'''' + @valuessql + n'''+' end + @crlf + @selectlist + case when @useselectsyntax = 1 n'' else n'+' + @crlf + n''')''' end + case when @generatestatementterminator = 1 n'+'';''' else n'' end + case when @generatego = 1 n'+' + @crlf + n'char(13)+char(10)+' + @crlf + n'''go''' else n'' end ; end else begin set @selectlist = case when @useselectsyntax = 1 n'''' + @selectsql + n'''+' else n'''(''+' end + @crlf + @selectlist + case when @useselectsyntax = 1 n'' else n'+' + @crlf + n''')''' end ; end set @selectstatement = n'select' + case when nullif(@topexpression,n'') not null n' top ' + @topexpression else n'' end + @crlf + @selectlist + @crlf + n'from ' + @objectname + case when nullif(@functionparameters,n'') not null @functionparameters else n'' end + case when nullif(@searchcondition,n'') not null @crlf + n'where ' + @searchcondition else n'' end + case when nullif(@orderbyexpression,n'') not null @crlf + n'order ' + @orderbyexpression else n'' end + @crlf + n';' + @crlf + @crlf ; if @debug = 1 begin print(@crlf + n'--select statement'); print(@selectstatement); end insert @tabledata execute (@selectstatement); if @generateprojectinfo = 1 begin insert @results select n'--inserts generated generateinsert (build 6)' union select n'' end if @generatesetnocount = 1 begin insert @results select n'set nocount on' end if @populateidentitycolumn = 1 begin insert @results select n'set identity_insert ' + coalesce(@targetobjectname,@objectname) + n' on' end if @generatesingleinsertperrow = 1 begin insert @results select tablerow @tabledata end else begin if @formatcode = 1 begin insert @results select @insertsql; if @ommitinsertcolumnlist != 1 begin insert @results select n'(' + @columnlist + n')'; end if @useselectsyntax != 1 begin insert @results select n'values'; end end else begin insert @results select @insertsql + case when @ommitinsertcolumnlist = 1 n'' else n' (' + @columnlist + n')' end + case when @useselectsyntax = 1 n'' else n' values' end end set @rowno = 0; declare datacursor cursor local fast_forward select tablerow @tabledata read ; open datacursor; fetch next datacursor @tablerow; while @@fetch_status = 0 begin set @rowno = @rowno + 1; insert @results select case when @useselectsyntax = 1 case when @rowno > 1 n'union' + case when @formatcode = 1 @crlf else n' ' end else n'' end else case when @rowno > 1 n',' else n' ' end end + @tablerow; fetch next datacursor @tablerow; end close datacursor; deallocate datacursor; if @generatestatementterminator = 1 begin insert @results select n';'; end if @generatego = 1 begin insert @results select n'go'; end end if @populateidentitycolumn = 1 begin insert @results select n'set identity_insert ' + coalesce(@targetobjectname,@objectname) + n' off' end if @formatcode = 1 begin insert @results select n''; -- empty line @ end end if @printgeneratedcode = 1 begin declare @longrows bigint; set @longrows = (select count(*) @results len(tablerow) > 4000); if @longrows > 0 , @showwarnings = 1 begin print(n'--*************************'); if @longrows = 1 print(n'--warning: ' + convert(nvarchar(max), @longrows) + n' row long , chopped @ every 4000 character.') else print(n'--warning: ' + convert(nvarchar(max), @longrows) + n' rows long , chopped @ every 4000 character.'); print(n'-- if issue workaround use @printgeneratedcode = 0 , output "result grid" in ssms.'); print(n'--*************************'); end declare resultscursor cursor local fast_forward select tablerow @results read ; open resultscursor; fetch next resultscursor @tablerow; while @@fetch_status = 0 begin declare @currentend bigint; -- track length of next sub-string declare @offset tinyint; -- tracks amount of offset needed set @tablerow = replace(replace(@tablerow, char(13) + char(10), char(10)), char(13), char(10)); while len(@tablerow) > 1 begin if charindex(char(10), @tablerow) between 1 , 4000 begin set @currentend = charindex(char(10), @tablerow) - 1; set @offset = 2; end else begin set @currentend = 4000; set @offset = 1; end print(substring(@tablerow, 1, @currentend)); set @tablerow = substring(@tablerow, @currentend + @offset, len(@tablerow)) end fetch next resultscursor @tablerow; end close resultscursor; deallocate resultscursor; end else begin select * @results; end end go ----============================== --once sp created run following code table_name.. declare @name nvarchar(261)='table_name'; declare tablecursor cursor local fast_forward select quotename(s.name) + '.' + quotename(t.name) objectname sys.tables t inner join sys.schemas s on s.schema_id = t.schema_id t.name not 'sys%' , t.name='table_name' read ; open tablecursor; fetch next tablecursor @name; while @@fetch_status = 0 begin execute dbo.generateinsert @objectname = @name; fetch next tablecursor @name; end close tablecursor; deallocate tablecursor;
Comments
Post a Comment