1

I want to create a database from existing one without taking any data from it (only schema). I know, I can create a script for this purpose using SQL Server Management Studio, but I need some transact SQL script doing this dynamically

5
  • Possible duplicate of SQL Server 2012 copy database without data or MS SQL 2008 - Create a copy of the database without the data or probably more. If those don't answer your question, presumably because of "I need some transact SQL script doing this dynamically", you should explain what that is supposed to mean. Commented Dec 7, 2017 at 13:29
  • Take a look here and here Commented Dec 7, 2017 at 13:31
  • Thanks for your replay HoneyBadger but this is not what I want.I want something like procedure or a function Commented Dec 7, 2017 at 13:37
  • How about creating another database with no data. Then, when you generate the script, use it to create a stored procedure in this db with no data. You might need some input parameters for the database name and location. Commented Dec 7, 2017 at 13:48
  • Thanks, Dan Bracuk this is a great idea but later if I had some changes in the schema of Database I need recreate script and alter my procedure.I don't want this. I want this happen dynamically Commented Dec 7, 2017 at 17:22

3 Answers 3

6

Try this DBCC command: DBCC CLONEDATABASE https://support.microsoft.com/en-us/help/3177838/how-to-use-dbcc-clonedatabase-to-generate-a-schema-and-statistics-only

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, PingPongOng .I think this is exactly what I need.
0

Were I to need to do this, this is the approach I'd use. This is not a trivial task and the built in scripting tools are better suited as has been noted. However, this is the approach I'd use if I had to do it.

Step 1 - Build tables - Build a cursor for all your tables using:

SELECT object_schema_name([object_id]), [name]
FROM   [sys].[objects]
WHERE  [type] IN ( N'U' )
ORDER  BY [name];

Step through each and build dynamic SQL to "SELECT * INTO .. from ..

Step 2 - Build procedures and functions - Build a cursor using:

SELECT [name]
FROM   [sys].[objects]
WHERE  [type] IN ( N'P', N'TF', N'FN', N'IF' )
ORDER  BY [name];

Step through the objects and run "exec sp_helptext '.'" on each. Catenate the resultant lines using a COALESCE string builder and execute the result. Execute the result on your target database.

1 Comment

This is not what I want. Thanks for your help
0

Right click on Databases, Tasks, Extract Data-tier Application:

enter image description here

After creating the dacpac file, right click on Databases and click on Deploy Data-tier Application

enter image description here

Select the dacpac created and it's done.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.