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


Generating Insert Statements


Generating Insert Statements

Author
Message
kodracon
kodracon
SSC-Enthusiastic
SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)

Group: General Forum Members
Points: 167 Visits: 162
i have written some code to help developers spend less time on scripting basic statements.

the first link is a demonstration of this:
[/url]

the second one is the actual code that uses metadata to easily script select, insert and update statements:

[url=http://www.sqlservercentral.com/scripts/AutoGenerate++++++parameters/64760/]


the next script is useful for deleting the statements produced by the above code, useful for when changes like a new table or column have been introduced and its time to redefine the stored procs to match:

[url=http://www.sqlservercentral.com/scripts/Stored+Procedures/65869/][/url]
kodracon
kodracon
SSC-Enthusiastic
SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)

Group: General Forum Members
Points: 167 Visits: 162
i have written some code to help developers spend less time on scripting basic statements.

the first link is a demonstration of this:
[/url]

the second one is the actual code that uses metadata to easily script select, insert and update statements:

[url=http://www.sqlservercentral.com/scripts/AutoGenerate++++++parameters/64760/]


the next script is useful for deleting the statements produced by the above code, useful for when changes like a new table or column have been introduced and its time to redefine the stored procs to match:

[url=http://www.sqlservercentral.com/scripts/Stored+Procedures/65869/][/url]
kodracon
kodracon
SSC-Enthusiastic
SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)SSC-Enthusiastic (167 reputation)

Group: General Forum Members
Points: 167 Visits: 162
i have written some code to help developers spend less time on scripting basic statements.

the first link is a demonstration of this:
[/url]

the second one is the actual code that uses metadata to easily script select, insert and update statements:

[url=http://www.sqlservercentral.com/scripts/AutoGenerate++++++parameters/64760/]


the next script is useful for deleting the statements produced by the above code, useful for when changes like a new table or column have been introduced and its time to redefine the stored procs to match:

[url=http://www.sqlservercentral.com/scripts/Stored+Procedures/65869/][/url]
Duncan A. McRae
Duncan A. McRae
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 66
Not only did I appreciate this script, I *USE* it. I made a slight change to shrink the output a tad (shown below). The negative to my approach is that I must manually delete the very last "union all" piece, but I can live with that. Thanks Boss!

set @script = 'select ''select '' + ' + substring(@values, 11, len(@values)) + ' + '') union all '' from ' + @table;


if ( @is_identity = 1 ) begin
print ('set identity_insert ' + @table + ' on');
end

print 'insert into ' + @table + ' (' + substring(@columns, 3, len(@columns)) + ')'
exec sp_executesql @script;

if ( @is_identity = 1 ) begin
print ('set identity_insert ' + @table + ' off');
end



Duncan A. McRae
Duncan A. McRae
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 66
And I'll likely page the resulting statements so I can keep inserts to batches of 1,000 at a time -- it works faster this way.



radityan
radityan
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 74
Excellent Article man...will be really helpful!!
sergio.eduardo.linares
sergio.eduardo.linares
SSC Rookie
SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)

Group: General Forum Members
Points: 33 Visits: 81
That is very interesting, bud i have a problem with "float" data type Unsure
misus28
misus28
SSC Rookie
SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)

Group: General Forum Members
Points: 40 Visits: 31
already done in ssms 2008, but nice try I might find it useful
ziangij
ziangij
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3569 Visits: 377
thank you :-)... i have been using this script from the time it was first published... is there any change from the first query ?
luke.baughan
luke.baughan
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 18
I've often used a useful stored procedure for this type of thing, also works like a charm! Feel free to check it out at http://vyaskn.tripod.com/code.htm direct links to the stored procedure source for 2000 is http://vyaskn.tripod.com/code/generate_inserts.txt , for 2005/8 its http://vyaskn.tripod.com/code/generate_inserts_2005.txt
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