When I right-click a table in Object Explorer and click "Script Table" I can get SSMS to script my table.

Can I do that with T-SQL code?

I'm after the "CREATE TABLE xx" code that SSMS generates, so that I can take the script and use on other projects. I need all the bits and pieces, including PRIMARY KEY, CONSTRAINTs, "WITH (PAD_INDEX = " and schema declaration.

I'm on SQL Server 2014

  • Can you please be more specific by what you mean by "T-sql code"? – Sir Swears-a-lot Apr 28 '15 at 9:34

From 2012 onwards this is possible and can be done with help OF sys.dm_exec_describe_first_result_set DMV.

This dynamic management function takes a Transact-SQL statement as a parameter and describes the metadata of the first result set for the statement

You can refer here for a read on this sys.dm_exec_describe_first_result_set

Also, refer to this post explaining the same. Is there a way to generate table create script in TSQL?

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.