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)

Get DDL for any SQL 2005 table

By Lowell Izaguirre,

The purpose of sp_GetDDL is to get the full DDL of any table in TSQL; a function version of the same code also exists.

since the results are in a single varchar(max), it makes it easy to use in the auditing of DDL triggers, or capturing an occasional schema snapshot for disaster recovery scnarios.

Behind the scenes it's smart, readable but somewhat complicated code. usage upfront is easy:

exec sp_GetDDL YourTableName
-- or

exec sp_GetDDL 'schemaname.tablename'

-- or

exec sp_GetDDL [schemaname].[tablename]


it produces well formatted CREATE TABLE scripts like this: note that a lot of testing went into the spacing to make everything have a sharp appearance;

The proc has been enhanced to fix minor issues, and recently added any extended properties for the table ror it's columns.

[STATETBLKEY]  INT                              NOT NULL,
[INDEXTBLKEY]  INT                              NOT NULL,
[STATECODE]    CHAR(2)                          NOT NULL,
[STATENAME]    VARCHAR(50)                      NOT NULL,
[FIPS]         CHAR(3)                              NULL,

Total article views: 1229 | Views in the last 30 days: 4
Related Articles

Get DDL for any SQL 2000 table

a stored procedure returning a single varchar(8000) of the CREATE TABLE script for any table.It does...


A little Help

Base on the code bellow, it actually works but the fact is that it does not show the complete create...


A way to script all DBs from a server

A way to script all DBs from a server


How to get Create script of an existing table

Hi, Is there any function which gives the table creation script. something like below create ...


how to check table creation statement

Hi All Sorry but silly question .that is their any command or dmv by which we can check create st...


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