Jeremy Davis
Jeremy Davis
Sitecore, C# and web development
Article printed from: https://blog.jermdavis.dev/posts/2024/scripting-data

Scripting your data with SSMS

I've had to look this up once too often...

Published 12 August 2024
SQL Server ~1 min. read

As developers we often need to move data between SQL Server instances. Some of the time database backups are fine for this, because you want to move everything. But on occasion what you need is a SQL script which can insert some rows of data you had on another server for you. And when I find myself needing this feature, I also find myself having to google how to do it. So to help my memory, here's how to generate scripts for your data:

Steps...

  • Fire up SQL Server Management Studio and drill down to your database. You'll be tempted to go down to the table level (because it's table data you want to script) but don't go that far. Stop at the database that contains your table.
  • Right-click your chosen database and select "Tasks" and "Generate Scripts" from the context menus: The SSMS window showing right-clicking a database, selecting 'Tasks' and 'Generate Scripts' from the resulting popup menus
  • Click "Next" through the welcome page of the "Generate Scripts" window if necessary.
  • Check the "select specific database objects" radio button, and find the table(s) you're interested in from the display of objects. Select the ones you want to script for: The wizard page for choosing database options, selecting the specific table(s) to export rows from Then click "Next".
  • On the options page of the wizard, you can pick whatever output target you want, but you need to click the "Advanced" button as well: The options page of the scripting wizard, showing the 'Advanced' button which you need to click to find the important options for the next step
  • Once the "Advanced Scripting Options" dialog opens, scroll down the list of settings to find "Types of data to script" at the end of the "General" section. Change this dropdown from "Schema only" to "Data Only", or if you need both "Schema and data": The 'Types of data to script' line in the 'Advanced Scripting Options' window - where you can specify a 'Data Only' export. You may have other settings you want to make here - but this is the important one. And then you can click OK.
  • And finally click "Next" a couple of times to start the export process. The scripting wizard's confirmation page showing it has successfully generated a script for your data And finally, click "Finish" to close the wizard.

And that will get you a script file which inclues INSERT statements for all the data rows in your chosen table(s). You can then edit that script to be contain just the data you need, or add in other bits and pieces as required.

It's pretty simple. I'm not sure why I always have so much trouble remembering how to do this - other than blundering around trying to work out why I can't find a sensibly named option on the individual tables. But no excuses now...

↑ Back to top