9

I need to create the SQL code that in it's output will return the CREATE TABLE SQL for all tables in current database.

Any suggestions?

Dale K
  • 25,246
  • 15
  • 42
  • 71
IgO
  • 103
  • 1
  • 2
  • 8
  • Is there an analog of MySQL's "SHOW CREATE TABLE" that you can use? – SteJ Sep 16 '15 at 11:52
  • possible duplicate of [Create SQL script that create database and tables](http://stackoverflow.com/questions/5771047/create-sql-script-that-create-database-and-tables) – Rahul Tripathi Sep 16 '15 at 11:55
  • Hmmm - seems not -- see [here](http://stackoverflow.com/questions/3526982/show-create-table-tablename-how-do-i-do-this-in-sql-server) – SteJ Sep 16 '15 at 11:55

2 Answers2

33

In SQL Server Management Studio(SSMS)

Right click on the DB_NAME -> Select Task -> Select Generate Script.

Follow along the presented wizard and select all tables in that database to generate the scripts.

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • This doesn't answer the OP question. They were looking for a SQL script to perform this action. – Joshua Feb 05 '23 at 04:26
  • @DaleK See my comment on your answer. Any others that come looking, the answer is at https://stackoverflow.com/questions/706664/generate-sql-create-scripts-for-existing-tables-with-query – Joshua Feb 05 '23 at 20:49
0

Use a Visual Studio SQL Server Database Project as follows:

  1. Create a Visual Studio SQL Server Database Project.

enter image description here

  1. From the Project Menu, select Import > Database ...

  2. Right click on the project and click publish.

Dale K
  • 25,246
  • 15
  • 42
  • 71
  • 1
    Thank you for this answer. I really love the ability to create database projects from existing databases. You can even track changes between your prod/dev DBs. Totally underrated answer! – Jonas Jun 17 '20 at 00:40
  • This doesn't answer the OP question. They were looking for a SQL script to perform this action. – Joshua Feb 05 '23 at 04:26
  • @DaleK I think you misunderstood the question. The OP wanted a SQL script that would generate the create scripts. Anyone could use SSMS or Visual Studio, but that doesn't satisfy unattended scripting needs. The correct answer can be found at https://stackoverflow.com/questions/706664/generate-sql-create-scripts-for-existing-tables-with-query – Joshua Feb 05 '23 at 20:48
  • @DaleK Are you serious?? "I need to create the SQL code that in it's output will return the CREATE TABLE SQL for all tables in current database." What part of that don't you understand? He needed a SQL script. That's not my opinion; that was the request. – Joshua Feb 06 '23 at 21:29