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

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Powershell/SMO: Unload table schema, dri, perms & data.

By Gordon-265412,


.\Script_Table_to_File.ps1 FooServer BarDB 'Foo Table' 'X:\Foo Dir\'


This was merely a personal exersize that got a little bit beyond that. :)

Tables and output paths that have spaces in them must be encapsulated in single-tick apostrophes on the command line call.

This script will produce two files in a datestamped subdirectory of the $outroot values (UNC paths will work as well). One script is the table schema, which includes DRI and permissions on the object. The second file is the actual data unloaded from the table in a pipe ("|") delimited format.

### NOTE ###: Once the table has been successfully scripted and unloaded it is dropped.

Currently this script only finds dbo owned objects. I suspect I'll need to re-write a good piece of it to find non-dbo objects....

This script will execute against any version of SQL server that I've tested (2000, 2005 & 2008).

Total article views: 957 | Views in the last 30 days: 1
Related Articles

Scripting Objects Including Permissions

Scripting Objects Including Permissions


SSIS script object

Script object Design Script does not open up script editor


Generating Scripts for Database Objects

Creating Seperate Scripts Per Object


Scripting All Objects from SSMS in to a VCS

A short tutorial on scripting objects in SSMS.


Scripting objects in SSMS (2008)

Scripted objects are being wrapped in dynamic SQL