Sometimes you need to be able to script the data out of a table and it is easier than you might think.

I commonly do this when I am generating automated tests or updating a product with system data for deployment.

In a future post I will go through how I use this in conjunction with our product testing suites

For this example, let’s assume I am scripting out some system configuration values for a current product to ship with the release.

Here’s how:

  1. Open SSMS (SQL Server Management Studio)
  2. Right Click on the Database that contains your data and select Tasks followed by Generate Scripts

GenerateScriptsMachine generated alternative text: IPS PET IPS PET Stag,ng New Database... New Query Script Database as Tasks Policies Fa cets Stat PowerSheII Reports Rename Delete Refresh Properties Detach... Take Offline Bring Online Shrink Back Up... Restore Mirror... Launch Database Mirroring Monitor... Ship Transaction Logs.. Generate Scripts.. Extract Data-tier Application... Register as Data-tier Application... Import Data... Export Data... Copy Database... Manage Database Encryption...

  1. Click Next on the Wizard Introduction Page
  2. On the Choose objects you can select the objects you would like to script out of the database. We will select “Select specific database objects” and then under tables select the table we are interested in scripting data from – [CONFIG].[GeneralSettings]
  3. GS_ObjectSelectMachine generated alternative text: Generate and Publish Scripts Choose Objects Introduction Choose Objects Set Scripting Options Summary Save or Publish Scripts tyæ dä*æe to Script entire database and all database objects Select specific database objects APP System Errors Application APP System Emoæ Database D CONFIG&sePage CONFIGCoIumnDispIay CONFIG GeneralSettings CO N FIG ManualOvemdeSettings Help Finish Select Al Deselect Al Previous
  4. Click Next
  5. Now we will select our scripting options. By default you would normally just get the object creation script, but this is where we can specify we want the data. Click on the “Advanced” button to bring up the Advanced Scripting Options dialogue.

GS_AdvancedOptionsMachine generated alternative text: Advanced Scripting Options ANSI Padding Append to File Continue scripting on Emor Convert LIDDTsto Base Types Generate Script for Dependent Objects Include Descriptive Headers Include f NOT EXISTS Include system constraint names Include unsupported statements Schema qualify object names Script Bindings Script Collation Script Defautts script DROP and CREATE Script Properties Script for Server Version Script for the database engine type Script Logins Script Object -Level Permissions Script Statistics script USE DATABASE Types of data to script Script Change Tracking Script Check Constraints Script Data Compression Options Script Foreign Keys Script hdexes Script Indexes Script Primary Keys Script Triggers Script Unique Keys ANSI Generates ANSI padding statements False False False False True False False False True False False True script CREATE True SQL Server 2008 R2 Stand-alone instance False False Do not script statistics True Schema only False True False True False False True False

  1. At the very bottom of the General section we want to change the “Types of data to script” from “Schema Only”. Normally, I would probably just select “Data Only”, but for this example I will choose “Schema and Data” so you can see what both provide.

GS_DataOptionMachine generated alternative text: schpt USE DATABASE Types of data to script T*e/Mew Script Change Tracking Script Check Constraints Schot Data Com True Schema and data Data only Schema and data Schema only

  1. Click “OK” to return the Script Options page.
  2. Now select how you want to output the script. If it is a relatively small amount of data (hundreds of rows or less) then I usually choose “Save to new query window”. You could also choose “Save to clipboard” and then paste it where you want. If you are dealing with a large amount of data (thousands of rows or more) you may want to leave the “Save to file” option selected. For our example choose “Save to new query window”

GS_OutputSelectMachine generated alternative text: Save to Clipboard @ Save to new query window Previous Finish

  1. Click Next on the Scripting Options page
  2. Click Next on the Summary page
  3. You will get a confirmation page similar to the following.

GS_DoItMachine generated alternative text: Generate and Publish Scripts Save or Publish Scripts Introduction Choose Objects Savirw or Set Scripting Options Summary Save or Publish Scripts Prepanng CONFIG General Settings Save to new query window Previous Help Save Report

  1. You should have gotten a New Query in management studio that contains the requested results.

GS_OutputMachine generated alternative text: USE SET SET SET LIPS PET Dashboard) Object: Table (CONFIG) ANSI NULLS ON QUOTED IDENTIFIER ON ANSI PADDING ON (General Settings) Script Date : 04/21/2016 07: 21 CREATE TABLE (SettingGrcup) (varchar) 2 S ) NULL, (SettingName) (varchar) 100 ) NULL, (SettingVaIue) Invarchar) ISO ) NULL, (LastChangeUser) (varchar) 20 ) NULL, CONSTRAINT (PK CONFIG Generalsettings) PRIMARY KEY CLUSTERED (SettingGroup) ÄSC, (SettingName) ASC STATISTICS 1 GNORE WITH PAD INDEX ) ON (PRIMARY) SET ANSI PADDING ON OFF NORECOMPUTE OFF OFF ALLOW VALUE S VALUE S VALUE S VALUE S VALUE S VALUE S VALUE S VALUE S VALUE S VALUE S VALUE S VALUE S PAGE LOCKS (PRIMARY) N'Ä110w Data Export', N 'True', N 'DEFAULT') data import N' DEFAULT') DUP KEY ALLOW ROW LOCKS INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT (CONFIG) (CONFIG) (CONFIG) (CONFIG) (CONFIG) (CONFIG) (CONFIG) (CONFIG) (CONFIG) (CONFIG) (CONFIG) (CONFIG) Object: (General Settings) (General Settings) (General Settings) (General Settings) (General Settings) (General Settings) (General Settings) (General Settings) (General Settings) (General Settings) (General Settings) (General Settings) (SettingGroup) IS e t tingGrcup) IS e t tingGrcup) IS e t tingGrcup) IS e t tingGrcup) IS e t tingGrcup) IS e t tingGrcup) IS e t tingGrcup) IS e t tingGrcup) IS e t tingGrcup) IS e t tingGrcup) ( (SettingGrcup) (SettingName ) (Set tingName) (Set tingName) (Set tingName) (Set tingName) (Set tingName) (Set tingName) (Set tingName) (Set tingName) (Set tingName) (Set tingName) (Set tingName) (SettingVaIue ) (SettingVaIue) (SettingVaIue) (SettingVaIue) (SettingVaIue) (SettingVaIue) (SettingVaIue) (SettingVaIue) (SettingVaIue) (SettingVaIue) (SettingVaIue) (SettingVaIue) (LastChangeUser) (LastChangeUser) (LastChangeUser) (LastChangeUser) (LastChangeUser) (LastChangeUser) (LastChangeUser) (LastChangeUser) (LastChangeUser) (LastChangeUser) (LastChangeUser) (LastChangeUser) Controls ' DataHeaIth', N 'Last ProcessTimeNazningMessage', N' Nazni:w: The DataHeaIth', N 'Last ProcessTimeNazningThzeshoIdMinutes', N' 10' ETL ' ETL ' ETL ' ETL ' N N' DEFAULT') N' ÄIIowTzackingNurEezsEzomGaps', N' False', N' DEFAULT') N 'Enablecachecleazing', N' False', N ' DEFAULT') N N ' DEFAULT') Reporti:w', N' DashboardPagingSpeedSeconds', N' S', N' ' System', Test i n; ' N 'PET Dashboard', N ' DEFAULT' N' False', N 'DEFAULT') N' Simulationoate', N' ' N' DEFAULT') N' Testi:wEnvizonmentELag', N' ' script Date: 04/21/2016 Default IDE CONFIG User) ALTER TABLE (CONFIG) , (Generalsettings) ADD CONSTRAINT (DE CONFIG Generalsettings User) DEFAULT ( FOR (Las tChangeUser)

I can now copy the data inserts and use them as defaults or automated testing data, etc.

Another Tip: if you are actually scripting the objects and want to do perform an existence check change the “Include IF NOT EXISTS” flag

Do you have any interesting uses for this?

Let us know in the comments below.