Sometimes you want to generate an INSERT script from existing data, perhaps with one or two values tweaked. It’s a pain to hand-crank the INSERT when you can see exactly what you want using a SELECT statement. It’s also a bit long-winded to use SQL import/export wizards or other tools. Sometimes, you just want to convert a SQL SELECT into an INSERT script. Here’s how. Show Change the three user variables at the top to say
Result (it doesn’t come out looking “pretty”, but I made it readable here by adding whitespace).
There are some limitations. This script isn’t going to check your column types, so everything is gonna be a string. SQL usually does alright with this, but you might find some loss of precision in datetime fields. It also doesn’t append the additional code to set identity insert on. I might add this later. Written by Steve Fenton Steve Fenton is an Octonaut at Octopus Deploy and five-time Microsoft MVP for developer technologies. He’s a Software Punk and writer. |