I need to be able to get the CREATE scripts for existing tables within SQL Server 2008. I assume I can do this by querying the sys.tables somehow, however this isn't returning me the CREATE script data.
相关问题
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- What is the best way to cache a table from a (SQL)
- php PDO::FETCH_ASSOC doesnt detect select after ba
do you mean you wish to create a TSQL script which generates a CREATE script, or use the Management tools in SQL SERVER Management Studio to generate a Create script?
If it's the latter, it's a simply matter of right-clicking a table, and selecting Script Table As -> Create To -> New Query Window.
If you want the whole database scripted, then right click the database and select Tasks--> Generate Scripts... and then follow the wizard
otherwise it's a matter of selecting all sorts of fun things out of the various system tables.
I realize this question is old, but it recently popped up in a search I just ran, so I thought I'd post an alternative to the above answer.
If you are looking to generate
create
scripts programmatically in .Net, I would highly recommend looking into Server Management Objects (SMO) or Distributed Management Objects (DMO) -- depending on which version of SQL Server you are using (the former is 2005+, the latter 2000). Using these libraries, scripting a table is as easy as:Here is a blog post with more information.
Here's a slight variation on @Devart 's answer so you can get the CREATE script for a temp table.
Please note that since the @SQL variable is an
NVARCHAR(MAX)
data type you might not be able to copy it from the result using just only SSMS. Please see this question to see how to get the full value of a MAX field.Since we're offering alternatives to what you asked..
If you're in .Net, you should look at the Database Publishing Wizard in Visual Studio. Easy way to script your tables/data to a text file.
http://www.codeplex.com/sqlhost/Wiki/View.aspx?title=Database%20Publishing%20Wizard
See my answer in this Question : How to generate create script of table using SQL query in SQL Server
Use this query :
And you can fire this
Function
like this :Possible this be helpful for you. This script generate indexes, FK's, PK and common structure for any table.
For example -
DDL:
Query:
Output:
Also check this article -
How to Generate a CREATE TABLE Script For an Existing Table: Part 1