SQL Server 2012 copy database without data
SQL Server 2012 copy database without data
回答1
- Right click your database in your server, and select
Tasks > Generate Scripts
. - At Introduction step, click Next.
- At Choose Objects, you can either select either the entire database (including functions, and etc) or specify what objects you want to generate. In your case, you should select
Script entire databases and all database objects
then hit Next. - Choose your file name and destination then click next
- Review and then click next
- Done!
You will see a .sql file generated in your specified folder. You can open that file in other servers and hit execute, it will then generate a exact same database.
回答2
You are missing the most important answer here: Introduced with MSSQL 2014 SP2 (2016-Jul-11), but later added to MSSQL 2012 with SP4 (2017-Oct-05) - the fastest and easiest way of preparing empty clone of your database is achievable via a DBCC CLONEDATABASE. Then according to the books online:
- Creates a new destination database that uses the same file layout as the source but with default file sizes from the model database.
- Creates an internal snapshot of the source database.
- Copies the system metadata from the source to the destination database.
- Copies all schema for all objects from the source to the destination database.
- Copies statistics for all indexes from the source to the destination database.
SYNTAX:
DBCC CLONEDATABASE
(
[Source_DB_Name]
,[Target_DB_Name]
)
WITH NO_STATISTICS, NO_QUERYSTORE, VERIFY_CLONEDB, BACKUP_CLONEDB
REMARKS:
- The source database must be a user database. Cloning of system databases (master, model, msdb, tempdb, distribution database etc.)
isn't allowed.The source database must be online or readable.
A database that uses the same name as the clone database must not already exist.
The command isn't in a user transaction.
- Requires SA server role
Here is a nice MS article about how to use it.
Second option:
Is to use a PowerShell module called dbatools.io which can be sourced directly form the project website or from the official PowerShell Gallery.
Once you have it, you can use this command:
Invoke-DbaDbClone
[-SqlInstance] <DbaInstanceParameter[]>]
[-SqlCredential] <PSCredential>]
[-Database] <String[]>]
[-InputObject] <Database[]>]
[-CloneDatabase] <String[]>]
[-ExcludeStatistics]
[-ExcludeQueryStore]
[-UpdateStatistics]
[-EnableException]
[-WhatIf]
[-Confirm] [<CommonParameters>]
As the dbatools.io is an open source project you can see what exactly happens in the background thanks to their publicly available GitHub repo.
And what you will find there is that they use DBCC CLONEDATABASE() to perform the core of this operation (but giving you ability to do much more).
回答3
You could generate the script for the database objects by right clicking it in Management Studio, then going to Tasks, Generate Scripts, choosing the objects you want to script, such as Tables, Views, etc. and there you have it.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2021-06-21 What is the difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout?
2021-06-21 Reference Microsoft.SqlServer.Smo.dll
2021-06-21 Execute a large SQL script (with GO commands)
2021-06-21 For SameSite cookie with subdomains what are considered the same site?
2021-06-21 How to handle multiple cookies with the same name?
2021-06-21 How can I show the table structure in SQL Server query?
2021-06-21 SQL Server Cursor Explained By Examples