﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Bharat Panthee  / Store Procedure to create Insert statement from exisiting records / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 04:23:19 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Store Procedure to create Insert statement from exisiting records</title><link>http://www.sqlservercentral.com/Forums/Topic1044189-2868-1.aspx</link><description>I keep getting below error for 4-5 tables I tried to generate the insert statementsMsg 245, Level 16, State 1, Procedure GenerateInsertStatement, Line 72Conversion failed when converting the varchar value 'WXPAY0000001578' to data type int.</description><pubDate>Tue, 15 May 2012 15:43:58 GMT</pubDate><dc:creator>jay_nagda</dc:creator></item><item><title>RE: Store Procedure to create Insert statement from exisiting records</title><link>http://www.sqlservercentral.com/Forums/Topic1044189-2868-1.aspx</link><description>I like the script.  I would like to see the datetime fields formatted using format 121 so that the second and millisecond precision is retained.</description><pubDate>Mon, 10 Jan 2011 13:18:50 GMT</pubDate><dc:creator>fahey.jonathan</dc:creator></item><item><title>RE: Store Procedure to create Insert statement from exisiting records</title><link>http://www.sqlservercentral.com/Forums/Topic1044189-2868-1.aspx</link><description>Very good suggestion, thank you :-)</description><pubDate>Sat, 08 Jan 2011 02:49:56 GMT</pubDate><dc:creator>Bharat Panthee</dc:creator></item><item><title>RE: Store Procedure to create Insert statement from exisiting records</title><link>http://www.sqlservercentral.com/Forums/Topic1044189-2868-1.aspx</link><description>I use SSMS Tools Pack for this. [url]http://www.ssmstoolspack.com/[/url]</description><pubDate>Fri, 07 Jan 2011 09:27:38 GMT</pubDate><dc:creator>rVadim</dc:creator></item><item><title>RE: Store Procedure to create Insert statement from exisiting records</title><link>http://www.sqlservercentral.com/Forums/Topic1044189-2868-1.aspx</link><description>I would love to use it, but I'm getting the following error messages:Msg 16916, Level 16, State 1, Procedure GenerateInsertStatement, Line 70A cursor with the name 'rec_col' does not exist.Msg 16916, Level 16, State 1, Procedure GenerateInsertStatement, Line 72A cursor with the name 'rec_col' does not exist.Msg 16916, Level 16, State 1, Procedure GenerateInsertStatement, Line 126A cursor with the name 'rec_col' does not exist.Msg 16916, Level 16, State 1, Procedure GenerateInsertStatement, Line 127A cursor with the name 'rec_col' does not exist.I see where the cursor is being created, but not sure why it's not working.</description><pubDate>Fri, 07 Jan 2011 09:03:56 GMT</pubDate><dc:creator>pwnies</dc:creator></item><item><title>RE: Store Procedure to create Insert statement from exisiting records</title><link>http://www.sqlservercentral.com/Forums/Topic1044189-2868-1.aspx</link><description>Okay that is one horribly slow way of doing that. Now i dont have a script for doing this myself so creating one from scratch i have dropped some stuff that should be there. Like handling of different datatypes. Possibly you would want to exclude some columns (like identity). But this wouldnt effect performance (or would make it faster... less columns :-)). But i came up with this.[code="sql"]declare @table varchar(128)select @table = 'YourTableHere'declare @Columns table (column_id integer primary key, name varchar(128), type varchar(128))create table #result (RowNr integer primary key, str varchar(max))insert into @Columns (column_id, name, type)select ordinal_position, Column_name, data_type from information_schema.columns where table_name = @table--In order to be able to sort the data exactly the same way every time we get the PK so we can sort on thatdeclare @pk varchar(max)select @pk = (select ', ' + c.name               from sys.key_constraints as k              join sys.tables as t on t.object_id = k.parent_object_id              join sys.index_columns as ic on ic.object_id = t.object_id and ic.index_id = k.unique_index_id              join sys.columns as c on c.object_id = t.object_id and c.column_id = ic.column_id              where t.name = @table and  k.type = 'PK'              order by ic.key_ordinal              for xml path(''), TYPE).value('.', 'varchar(max)')select @pk = SubString(@pk, 3, Len(@pk))--Build the start of the insert stringdeclare @insertstring varchar(max)select @insertstring = (select name + ', ' from @Columns order by column_id for xml path (''), TYPE).value('.', 'varchar(max)')select @insertstring = 'insert into ' + @table + ' (' +  SubString(@insertstring, 1, Len(@insertstring) - 2) + ') values ('--Okay time to build the result one column at a timedeclare @i integerdeclare @sql varchar(max)set @i = 0while exists (select * from @Columns where column_id &amp;gt; @i)begin  set @i = @i + 1  --First time we have to insert instead of update  if @i = 1  begin    select @sql = ';with cte as (select row_number() over (order by ' + @pk + ') RowNr, Convert(varchar(max), ' + name + ') str from ' + @table + ') ' +                   'insert into #Result (RowNr, str) select RowNr, str from cte'    from @Columns where column_id = @i        exec (@sql)  end    --Add the next column to the result  if @i &amp;gt; 1   begin    select @sql = ';with cte as (select row_number() over (order by ' + @pk + ') RowNr, Convert(varchar(max), ' + name + case when type = 'datetime' then ', 121' else '' end + ') str from ' + @table + ') ' +                   'update r set str = r.str + '', '' + IsNull('''''''' + cte.str + '''''''', ''NULL'') from #result r join cte on cte.RowNr = r.RowNr'    from @Columns where column_id = @i        exec (@sql)  endendselect RowNr, @insertstring + str + ')' from #resultdrop table #result[/code]Have to excuse the poor formating. Now i did do a WHILE instead of a cursor. Either one i think would be okay in this circumstance since the nr of columns are so few. The resulting insert command between the posted procedure and the above code are almost identical (differs a bit in formating). The HUGE difference is time. I ran both against a table with about 5000 rows (so a small one) with about 35 columns. And the procedure takes about 6min to finish and the above code about 7s (with result returned to the client). I almost guarantee that there are even faster solutions (because mine are normally not among the fastest... but fast enough for me :-)). So i eagerly await that :-D/T</description><pubDate>Fri, 07 Jan 2011 07:18:02 GMT</pubDate><dc:creator>tommyh</dc:creator></item><item><title>RE: Store Procedure to create Insert statement from exisiting records</title><link>http://www.sqlservercentral.com/Forums/Topic1044189-2868-1.aspx</link><description>You can add source and destination table's schema as parameter of SP and use there in code.</description><pubDate>Fri, 07 Jan 2011 05:13:48 GMT</pubDate><dc:creator>Bharat Panthee</dc:creator></item><item><title>RE: Store Procedure to create Insert statement from exisiting records</title><link>http://www.sqlservercentral.com/Forums/Topic1044189-2868-1.aspx</link><description>It doesn't appear to work with non dbo schema tables. My attempts to fix it on my own have so far been fruitless.Can anybody else shed some light on this?Thanks</description><pubDate>Fri, 07 Jan 2011 03:59:29 GMT</pubDate><dc:creator>daveyhodge</dc:creator></item><item><title>Store Procedure to create Insert statement from exisiting records</title><link>http://www.sqlservercentral.com/Forums/Topic1044189-2868-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/insert/72000/"&gt;Store Procedure to create Insert statement from exisiting records&lt;/A&gt;[/B]</description><pubDate>Fri, 07 Jan 2011 00:37:34 GMT</pubDate><dc:creator>Bharat Panthee</dc:creator></item></channel></rss>