Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
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)

T-SQL to Export Table Structure to a script

By Lowell Izaguirre,

When you want to export a table structure to T-SQL, everyone always points you to Enterprise Manager, or a script that calls BCP.
Sometimes, a developer either doesn't have access to those tools, or just doesn't want to use them (like me) out of spite.
I made this T-SQL because it was educational, and noone else seems to have done it yet.
ideally, with this script AND the sp_generate_inserts script from, you could use a loop to export both the definition of a table, And then it's contents.
please, if you take this script and make it better, please email me a copy at so I can learn from your improvements.

Best to view the results in Query Analyzer Results in Text (Control +T)

Known limitations at this time:
1.Doesn't add the check constraint's yet, only the implied PK AND Unique Constraints.
2.Doesn't add the indexes for the table yet.
3. Completely ignores the optional COLLATE statement because I never use it when creating a varchar; ie  COLLATE SQL_Latin1_General_CP1_CI_AS

Total article views: 3476 | Views in the last 30 days: 12
Related Articles

Monitoring script




Need Automated Script


drop script

drop script


Script component

Script component


Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones