2

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

Paul White
95.4k30 gold badges440 silver badges689 bronze badges
asked Apr 28, 2015 at 9:03
1
  • Can you please be more specific by what you mean by "T-sql code"? Commented Apr 28, 2015 at 9:34

1 Answer 1

1

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?

answered Apr 28, 2015 at 9:20

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.