Restore master database into newly named database with .bak file - sql-server

I wrote a script to restore to a new database that already has populated tables (basically the master database contains the same tables as the school database but one table needs renaming as it was renamed incorrectly in master). The .bak file was from the master database, and i'm trying to restore it to the new database, however I'm getting an error.
(2 rows affected)
-------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------
master mastlog
(1 row affected)
C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\DATA
C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\DATA
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\DATA
(1 row affected)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\DATA
(1 row affected)
Msg 3176, Level 16, State 1, Line 59
File '' is claimed by 'mastlog'(2) and 'master'(1). The WITH MOVE clause can be used to relocate one or more files.
Msg 3013, Level 16, State 1, Line 59
RESTORE DATABASE is terminating abnormally.
And this is the script:
use school;
DECLARE #TableSchema sys.sysname = N'dbo'
DECLARE #TableName sys.sysname = N'rights'
DECLARE #OldTableName sys.sysname = N'rigths'
DECLARE #OldTableWithSchema NVARCHAR(256) = QUOTENAME(#TableSchema) + '.' + QUOTENAME(#OldTableName)
DECLARE #TableWithSchema NVARCHAR(256) = QUOTENAME(#TableSchema) + '.' + QUOTENAME(#TableName)
IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = #TableSchema
AND TABLE_NAME = #TableName))
BEGIN
EXEC sp_rename #TableWithSchema, #OldTableName
END
DECLARE #Table TABLE ([LogicalName] varchar(128),[PhysicalName] varchar(128), [Type] varchar, [FileGroupName] varchar(128), [Size] varchar(128),
[MaxSize] varchar(128), [FileId]varchar(128), [CreateLSN]varchar(128), [DropLSN]varchar(128), [UniqueId]varchar(128), [ReadOnlyLSN]varchar(128), [ReadWriteLSN]varchar(128),
[BackupSizeInBytes]varchar(128), [SourceBlockSize]varchar(128), [FileGroupId]varchar(128), [LogGroupGUID]varchar(128), [DifferentialBaseLSN]varchar(128), [DifferentialBaseGUID]varchar(128),
[IsReadOnly]varchar(128), [IsPresent]varchar(128), [TDEThumbprint]varchar(128), [SnapshotUrl]varchar(128)
)
DECLARE #Path varchar(1000)='C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\Backup\SQL2008backup.bak'
DECLARE #LogicalNameData varchar(128),#LogicalNameLog varchar(128)
INSERT INTO #table
EXEC('
RESTORE FILELISTONLY
FROM DISK=''' +#Path+ '''
')
SET #LogicalNameData=(SELECT LogicalName FROM #Table WHERE Type='D')
SET #LogicalNameLog=(SELECT LogicalName FROM #Table WHERE Type='L')
SELECT #LogicalNameData, #LogicalNameLog
use master;
declare #MasterData nvarchar(512)
exec master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer\Parameters', N'SqlArg0', #MasterData output
select #MasterData=substring(#MasterData, 3, 255)
select #MasterData=substring(#MasterData, 1, len(#MasterData) - charindex('\', reverse(#MasterData)))
print #MasterData
declare #MasterLog nvarchar(512)
exec master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer\Parameters', N'SqlArg2', #MasterLog output
select #MasterLog=substring(#MasterLog, 3, 255)
select #MasterLog=substring(#MasterLog, 1, len(#MasterLog) - charindex('\', reverse(#MasterLog)))
print #MasterLog
declare #NewDefaultData nvarchar(512)
select isnull(#NewDefaultData, CONVERT(nvarchar(512), #MasterData))
declare #NewDefaultLog nvarchar(512)
select isnull(#NewDefaultLog, CONVERT(nvarchar(512), #MasterLog))
SET DEADLOCK_PRIORITY 10
ALTER DATABASE school
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
RESTORE DATABASE school FROM DISK=#Path
WITH
MOVE #LogicalNameData TO #NewDefaultData,
MOVE #LogicalNameLog TO #NewDefaultLog,
REPLACE
IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = #TableSchema
AND TABLE_NAME = #OldTableName))
BEGIN
EXEC sp_rename #OldTableWithSchema, #TableName
END
Updated with new code but new error

The Master database is usually used for system tables and I have never seen it used for application tables. If by accident it was used you should restore the database under a different name and copy out the tables (and other user objects) to a user database. If you insist on using master (and as we say in Brooklyn only bad things can come from this) you need to do the following:
Put the database in single user mode (by using the -m parameter in the startup file)
From sqlcmd run this command (with the actual location of the backup file):
RESTORE DATABASE master
FROM DISK = 'c:\backups\master.bak'
WITH REPLACE;

You need to overwrite your current database with the backed up database. You can do this with the REPLACE option
The basic syntax is...
SET DEADLOCK_PRIORITY 10
ALTER DATABASE master
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
restore database master from disk = 'c:\somefile.bak'
with replace

Related

Can't disable change tracking on table via Sql Server Data Tools

I have change tracking enabled/disabled in table [MyTable] of database [MyDb] using below given queries.
Pre Deployment query of SSDT project to disable change tracking on the table-
--Disable Change Tracking
IF EXISTS ( SELECT 1
FROM sys.change_tracking_tables
WHERE object_id = OBJECT_ID('MyTable'))
Begin
ALTER TABLE [dbo].[MyTable]
DISABLE CHANGE_TRACKING
End
GO
Declare #DBNAME SYSNAME = DB_NAME()
, #Sql NVARCHAR(MAX);
IF EXISTS (SELECT 1
FROM sys.change_tracking_databases
WHERE database_id = DB_ID(#DBNAME))
Begin
SET #Sql = N'ALTER DATABASE '+ QUOTENAME(#DBNAME) + N'
SET CHANGE_TRACKING = OFF WITH ROLLBACK IMMEDIATE'
Exec sp_executesql #Sql
End
GO
Post Deployment query of SSDT project to enable change tracking on the table-
--Enable Change Tracking
Declare #DBNAME SYSNAME = DB_NAME()
, #Sql NVARCHAR(MAX);
IF NOT EXISTS (SELECT 1
FROM sys.change_tracking_databases
WHERE database_id = DB_ID(#DBNAME))
Begin
SET #Sql = N' ALTER DATABASE '+ QUOTENAME(#DBNAME) + N'
SET CHANGE_TRACKING = ON
(CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON) '
Exec sp_executesql #Sql
End
GO
IF NOT EXISTS ( SELECT 1
FROM sys.change_tracking_tables
WHERE object_id = OBJECT_ID('MyTable'))
Begin
ALTER TABLE [dbo].[MyTable]
ENABLE CHANGE_TRACKING WITH (TRACK_COLUMNS_UPDATED = ON)
End
GO
When I publish the SSDT project I got below given error in the publish result-
Altering Table [dbo].[MyTable]...
(114,1): SQL72014: .Net SqlClient Data Provider: Msg 4998, Level 16, State 1, Line 1 Change tracking is not enabled on table 'MyTable'.
(114,0): SQL72045: Script execution error. The executed script:
ALTER TABLE [dbo].[MyTable] DISABLE CHANGE_TRACKING;
An error occurred while the batch was being executed.
Reviewing the publish script I found that SSDT has already added a has section to disable change tracking at the database level in the start of publish script.
IF EXISTS (SELECT 1
FROM [sys].[databases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
ALTER DATABASE [$(DatabaseName)]
SET QUERY_STORE = OFF
WITH ROLLBACK IMMEDIATE;
END
GO
But as I know that without disabling change tracking on table level we cannot disable it on database level.
I stuck here that how can I remove the default change tracking provided by SSDT itself.

SQL Server 2014 Create DDL trigger on Create Table that creates a trigger for the table

I'm working with a third party tool that creates databases and tables. I would like a trigger on one of those tables being created. I thought I would try to create a server DDL trigger that fires when the table is created in a new database, which in turn creates a trigger on that table. I cannot add the trigger to the 'model' database because this table is created via the tool dynamically.
I've tried the following:
CREATE TRIGGER ddl_trig_createTable
ON ALL SERVER
FOR CREATE_TABLE
AS
DECLARE #databaseName varchar(255)
DECLARE #AffectedTables varchar(255)
SELECT #AffectedTables = EVENTDATA().value('(/EVENT_INSTANCE/ObjectName)[1]','nvarchar(100)')
IF (#AffectedTables IN ('DynamicallyCreatedTable'))
BEGIN
select #databaseName = CAST(eventdata().query('/EVENT_INSTANCE/DatabaseName[1]/text()') as NVarchar(128))
EXEC('CREATE TRIGGER ' + #databaseName + '.[dbo].[tgrDynamicTableTrigger]
ON
' + #databaseName + '.[dbo].[DynamicallyCreatedTable]
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON
-- trigger code here
END')
END
GO
Which produces the following error when the table is created;
Msg 166, Level 15, State 1, Line 1 'CREATE/ALTER TRIGGER' does not
allow specifying the database name as a prefix to the object name.
I tried changing the dynamic sql by replacing the fully qualified table name to attempt a 'use' statement:
--- 8< ---
EXEC('use ' + #databaseName + '
CREATE TRIGGER [dbo].[tgrDynamicTableTrigger]
ON
--- 8< ---
However, that produced the following error when the table was created:
Msg 111, Level 15, State 1, Line 2 'CREATE TRIGGER' must be the first
statement in a query batch.
Any ideas?
I'm using SQL Server 2014.
I believe that I have figured it out, thanks mostly to this answer.
Here's the code:
CREATE TRIGGER ddl_trig_createTable
ON ALL SERVER
FOR CREATE_TABLE
AS
DECLARE #statement nvarchar(max) = 'CREATE TRIGGER [dbo].[tgrDynamicTableTrigger]
ON
[dbo].[DynamicallyCreatedTable]
AFTER UPDATE
AS
BEGIN
-- trigger code here
END'
DECLARE #databaseName varchar(255)
DECLARE #AffectedTables varchar(255)
SELECT #AffectedTables = EVENTDATA().value('(/EVENT_INSTANCE/ObjectName)[1]','nvarchar(100)')
IF (#AffectedTables IN ('DynamicallyCreatedTable'))
BEGIN
SET #databaseName = CAST(eventdata().query('/EVENT_INSTANCE/DatabaseName[1]/text()') as NVarchar(128))
DECLARE #sql NVARCHAR(MAX) = QUOTENAME(#databaseName) + '.sys.sp_executesql';
EXEC #sql #statement;
END
GO

Table already exists in a new SQL Server database

I'm writing a script to create a bunch of tables in SQL Server. As I write the script, I want to create and delete the database. The problem is that the I get an error saying that the object already exists.
Here is my example script
DECLARE #db_name varchar(20);
DECLARE #command varchar(100);
SET #db_name='testdb';
SET #command='DROP DATABASE ' + #db_name
IF EXISTS(SELECT * FROM sys.databases WHERE name=#db_name)
exec(#command)
SET #command='CREATE DATABASE ' + #db_name
EXEC(#command)
--listing databaes
SELECT name from master.dbo.sysdatabases
-- using our database
SET #command='USE ' + #db_name
EXEC(#command)
PRINT 'listing tables'
SET #command = 'SELECT table_name FROM ' + #db_name + '.INFORMATION_SCHEMA.TABLES WHERE table_type = "base TABLE"'
EXEC(#command)
CREATE TABLE stuff(
name VARCHAR(30) PRIMARY KEY NOT NULL,
weight INT,
quantity INT)
and the output I get is
name
------------
master
tempdb
model
msdb
testdb
SW
(6 rows affected)
listing tables
table_name
Error:
Msg 2714, Level 16, State 6, Server Orange, Line 22
There is already an object named 'stuff' in the database.
I run this on a Linux mint machine, a freshly installed SQL Server, and I use sqlcmd. I guess I can put a drop/delete command before the creating the table, but this shouldn't happen to begin with. What is going on here?
When you execute a USE statement from dynamic SQL, the database context reverts back to the original database context (master?) when the executed batch completes. You'll need to add a USE to the CREATE TABLE script and execute it using dynamic SQL too:
SET #command = N'USE' + QUOTENAME(#db_name) + N';
CREATE TABLE stuff(
name VARCHAR(30) PRIMARY KEY NOT NULL,
weight INT,
quantity INT);
';
bind the create table statement inside a object existence check. like this
IF OBJECT_ID('stuff') IS NULL
BEGIN
CREATE TABLE stuff(
name VARCHAR(30) PRIMARY KEY--NOT NULL is not needed as the primary key does not allow NULL,
weight INT,
quantity INT)
END

Dropping and recreating databases in Microsoft SQL Server

I am experimenting and learning with Microsoft SQL Server 2008 R2 SP1.
I have a database where I made many experiments. Now I would like to drop and recreate it.
So I extract the creation script from database, I delete it and I use the script to recreate it.
To my surprise, all the tables, keys etc are still there.
How do I drop the database, so that I can rebuild the database from scratch?
USE master
IF EXISTS(select * from sys.databases where name='yourDBname')
DROP DATABASE yourDBname
CREATE DATABASE yourDBname
+1 to AnandPhadke for his part of the code
This code will close all active connections to the database and then drop it
WHILE EXISTS(select NULL from sys.databases where name='YourDBName')
BEGIN
DECLARE #SQL varchar(max)
SELECT #SQL = COALESCE(#SQL,'') + 'Kill ' + Convert(varchar, SPId) + ';'
FROM MASTER..SysProcesses
WHERE DBId = DB_ID(N'YourDBName') AND SPId <> ##SPId
EXEC(#SQL)
DROP DATABASE [YourDBName]
END
GO
CREATE DATABASE YourDBName
GO
SQL Server 2016 (and above) support one line and atomic(?) syntax DROP DATABASE IF EXISTS database_name
REF: https://msdn.microsoft.com/en-us/library/ms178613.aspx
Requiring the DBName to be typed more than once is error prone, at some point it'll be executed with inconsistent entries and unintended consequences.
The answers from AnandPhadke or Pierre with variable support would be preferred for me.
DECLARE #DBName varchar(50) = 'YourDatabaseName'
USE master
IF EXISTS(select * from sys.databases where name= #DBName)
EXEC('DROP DATABASE ' + #DBName)
EXEC('CREATE DATABASE ' + #DBName)
or
DECLARE #DBName varchar(50) = 'YourDatabaseName'
WHILE EXISTS(select NULL from sys.databases where name = #DBName )
BEGIN
DECLARE #SQL varchar(max)
SELECT #SQL = COALESCE(#SQL,'') + 'Kill ' + Convert(varchar, SPId) + ';' FROM MASTER..SysProcesses WHERE DBId = DB_ID(#DBName) AND SPId <> ##SPId
EXEC(#SQL)
EXEC('DROP DATABASE ' + #DBName)
END
GO
I extract the creation script from database
This extract the creation script for everything in the database (tables, keys etc). If you simply want to create an empty database, just run CREATE DATABASE <dbname>
This works best for me:
if exists (select name from sys.databases where name='YourDBName')
alter database YourDBName set single_user with rollback immediate
go
if exists (select name from sys.databases where name='YourDBName')
drop database YourDBName

How can I clone an SQL Server database on the same server in SQL Server 2008 Express?

I have an MS SQL Server 2008 Express system which contains a database that I would like to 'copy and rename' (for testing purposes) but I am unaware of a simple way to achieve this.
I notice that in the R2 version of SQL Server there is a copy database wizard, but sadly I can't upgrade.
The database in question is around a gig.
I attempted to restore a backup of the database I want to copy into a new database, but with no luck.
Install Microsoft SQL Management Studio, which you can download for free from Microsoft's website:
Version 2008
Microsoft SQL Management Studio 2008 is part of SQL Server 2008 Express with Advanced Services
Version 2012
Click download button and check ENU\x64\SQLManagementStudio_x64_ENU.exe
Version 2014
Click download button and check MgmtStudio 64BIT\SQLManagementStudio_x64_ENU.exe
Open Microsoft SQL Management Studio.
Backup original database to .BAK file (db -> Task -> Backup).
Create empty database with new name (clone). Note comments below as this is optional.
Click to clone database and open restore dialog (see image)
Select Device and add the backup file from step 3.
Change destination to test database
Change location of database files, it must be different from the original. You can type directly into text box, just add postfix. (NOTE: Order is important. Select checkbox, then change the filenames.)
Check WITH REPLACE and WITH KEEP_REPLICATION
Right-click the database to clone, click Tasks, click Copy Database.... Follow the wizard and you're done.
You could try to detach the database, copy the files to new names at a command prompt, then attach both DBs.
In SQL:
USE master;
GO
EXEC sp_detach_db
#dbname = N'OriginalDB';
GO
At Command prompt (I've simplified the file paths for the sake of this example):
copy c:\OriginalDB.mdf c:\NewDB.mdf
copy c:\OriginalDB.ldf c:\NewDB.ldf
In SQL again:
USE master;
GO
CREATE DATABASE OriginalDB
ON (FILENAME = 'C:\OriginalDB.mdf'),
(FILENAME = 'C:\OriginalDB.ldf')
FOR ATTACH;
GO
CREATE DATABASE NewDB
ON (FILENAME = 'C:\NewDB.mdf'),
(FILENAME = 'C:\NewDB.ldf')
FOR ATTACH;
GO
It turns out that I had attempted to restore from a backup incorrectly.
Initially I created a new database and then attempted to restore the backup here.
What I should have done, and what worked in the end, was to bring up the restore dialog and type the name of the new database in the destination field.
So, in short, restoring from a backup did the trick.
Thanks for all the feedback and suggestions guys
This is the script I use. A bit tricky but it works. Tested on SQL Server 2012.
DECLARE #backupPath nvarchar(400);
DECLARE #sourceDb nvarchar(50);
DECLARE #sourceDb_log nvarchar(50);
DECLARE #destDb nvarchar(50);
DECLARE #destMdf nvarchar(100);
DECLARE #destLdf nvarchar(100);
DECLARE #sqlServerDbFolder nvarchar(100);
SET #sourceDb = 'db1'
SET #sourceDb_log = #sourceDb + '_log'
SET #backupPath = 'E:\DB SQL\MSSQL11.MSSQLSERVER\MSSQL\Backup\' + #sourceDb + '.bak' --ATTENTION: file must already exist and SQL Server must have access to it
SET #sqlServerDbFolder = 'E:\DB SQL\MSSQL11.MSSQLSERVER\MSSQL\DATA\'
SET #destDb = 'db2'
SET #destMdf = #sqlServerDbFolder + #destDb + '.mdf'
SET #destLdf = #sqlServerDbFolder + #destDb + '_log' + '.ldf'
BACKUP DATABASE #sourceDb TO DISK = #backupPath
RESTORE DATABASE #destDb FROM DISK = #backupPath
WITH REPLACE,
MOVE #sourceDb TO #destMdf,
MOVE #sourceDb_log TO #destLdf
None of the solutions mentioned here worked for me - I am using SQL Server Management Studio 2014.
Instead I had to uncheck the "Take tail-log backup before restore" checkbox in the "Options" screen: in my version it is checked by default and prevents the Restore operation to be completed.
After unchecking it, the Restore operation proceeded without issues.
From SSMS :
1 - Backup original database to .BAK file (your_source_db -> Task -> Backup).
2 - Right clicking the "Databases" and 'Restore Database'
3 - Device > ... (button) > Add > select the your_source_db.bak
4 - In 'General' tab, in 'Destination' section, rename in 'Database' your_source_db to new_name_db
5 - In 'Files' tab, tick 'Relocate all files to folder',
Rename in 'Restore As' column the two lignes to keep consistency with new_name_db (.mdf, _log.ldf)
6 - In 'Options' tab, in 'Restore options' section, tick two fist options ('Overwrite...', 'Preserve...') and for 'Recovery state' : 'RESTORE WITH RECOVERY'
Make also sure that in 'Tail-Log backup' section options are unticked to avoid keeping source db in 'restoring state' !
Using MS SQL Server 2012, you need to perform 3 basic steps:
First, generate .sql file containing only the structure of the source DB
right click on the source DB and then Tasks then Generate Scripts
follow the wizard and save the .sql file locally
Second, replace the source DB with the destination one in the .sql file
Right click on the destination file, select New Query and Ctrl-H or (Edit - Find and replace - Quick replace)
Finally, populate with data
Right click on the destination DB, then select Tasks and Import Data
Data source drop down set to ".net framework data provider for SQL server" + set the connection string text field under DATA ex: Data Source=Mehdi\SQLEXPRESS;Initial Catalog=db_test;User ID=sa;Password=sqlrpwrd15
do the same with the destination
check the table you want to transfer or check box besides "source: ..." to check all of them
You are done.
If the database is not very large, you might look at the 'Script Database' commands in SQL Server Management Studio Express, which are in a context menu off the database item itself in the explorer.
You can choose what all to script; you want the objects and the data, of course. You will then save the entire script to a single file. Then you can use that file to re-create the database; just make sure the USE command at the top is set to the proper database.
In SQL Server 2008 R2, back-up the database as a file into a folder.
Then chose the restore option that appears in the "Database" folder.
In the wizard enter the new name that you want in the target database.
And choose restore frrom file and use the file you just created.
I jsut did it and it was very fast (my DB was small, but still)
Pablo.
The solution, based on this comment: https://stackoverflow.com/a/22409447/2399045 .
Just set settings: DB name, temp folder, db files folder.
And after run you will have the copy of DB with Name in "sourceDBName_yyyy-mm-dd" format.
-- Settings --
-- New DB name will have name = sourceDB_yyyy-mm-dd
declare #sourceDbName nvarchar(50) = 'MyDbName';
declare #tmpFolder nvarchar(50) = 'C:\Temp\'
declare #sqlServerDbFolder nvarchar(100) = 'C:\Databases\'
-- Execution --
declare #sourceDbFile nvarchar(50);
declare #sourceDbFileLog nvarchar(50);
declare #destinationDbName nvarchar(50) = #sourceDbName + '_' + (select convert(varchar(10),getdate(), 121))
declare #backupPath nvarchar(400) = #tmpFolder + #destinationDbName + '.bak'
declare #destMdf nvarchar(100) = #sqlServerDbFolder + #destinationDbName + '.mdf'
declare #destLdf nvarchar(100) = #sqlServerDbFolder + #destinationDbName + '_log' + '.ldf'
SET #sourceDbFile = (SELECT top 1 files.name
FROM sys.databases dbs
INNER JOIN sys.master_files files
ON dbs.database_id = files.database_id
WHERE dbs.name = #sourceDbName
AND files.[type] = 0)
SET #sourceDbFileLog = (SELECT top 1 files.name
FROM sys.databases dbs
INNER JOIN sys.master_files files
ON dbs.database_id = files.database_id
WHERE dbs.name = #sourceDbName
AND files.[type] = 1)
BACKUP DATABASE #sourceDbName TO DISK = #backupPath
RESTORE DATABASE #destinationDbName FROM DISK = #backupPath
WITH REPLACE,
MOVE #sourceDbFile TO #destMdf,
MOVE #sourceDbFileLog TO #destLdf
Another way that does the trick by using import/export wizard, first create an empty database, then choose the source which is your server with the source database, and then in the destination choose the same server with the destination database (using the empty database you created at first), then hit finish
It will create all tables and transfer all the data into the new database,
Script based on Joe answer (detach, copy files, attach both).
Run Managment Studio as Administrator account.
It's not necessary, but maybe access denied error on executing.
Configure sql server for execute xp_cmdshel
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE
GO
Run script, but type your db names in #dbName and #copyDBName variables before.
USE master;
GO
DECLARE #dbName NVARCHAR(255) = 'Products'
DECLARE #copyDBName NVARCHAR(255) = 'Products_branch'
-- get DB files
CREATE TABLE ##DBFileNames([FileName] NVARCHAR(255))
EXEC('
INSERT INTO ##DBFileNames([FileName])
SELECT [filename] FROM ' + #dbName + '.sys.sysfiles')
-- drop connections
EXEC('ALTER DATABASE ' + #dbName + ' SET OFFLINE WITH ROLLBACK IMMEDIATE')
EXEC('ALTER DATABASE ' + #dbName + ' SET SINGLE_USER')
-- detach
EXEC('EXEC sp_detach_db #dbname = ''' + #dbName + '''')
-- copy files
DECLARE #filename NVARCHAR(255), #path NVARCHAR(255), #ext NVARCHAR(255), #copyFileName NVARCHAR(255), #command NVARCHAR(MAX) = ''
DECLARE
#oldAttachCommand NVARCHAR(MAX) =
'CREATE DATABASE ' + #dbName + ' ON ',
#newAttachCommand NVARCHAR(MAX) =
'CREATE DATABASE ' + #copyDBName + ' ON '
DECLARE curs CURSOR FOR
SELECT [filename] FROM ##DBFileNames
OPEN curs
FETCH NEXT FROM curs INTO #filename
WHILE ##FETCH_STATUS = 0
BEGIN
SET #path = REVERSE(RIGHT(REVERSE(#filename),(LEN(#filename)-CHARINDEX('\', REVERSE(#filename),1))+1))
SET #ext = RIGHT(#filename,4)
SET #copyFileName = #path + #copyDBName + #ext
SET #command = 'EXEC master..xp_cmdshell ''COPY "' + #filename + '" "' + #copyFileName + '"'''
PRINT #command
EXEC(#command);
SET #oldAttachCommand = #oldAttachCommand + '(FILENAME = "' + #filename + '"),'
SET #newAttachCommand = #newAttachCommand + '(FILENAME = "' + #copyFileName + '"),'
FETCH NEXT FROM curs INTO #filename
END
CLOSE curs
DEALLOCATE curs
-- attach
SET #oldAttachCommand = LEFT(#oldAttachCommand, LEN(#oldAttachCommand) - 1) + ' FOR ATTACH'
SET #newAttachCommand = LEFT(#newAttachCommand, LEN(#newAttachCommand) - 1) + ' FOR ATTACH'
-- attach old db
PRINT #oldAttachCommand
EXEC(#oldAttachCommand)
-- attach copy db
PRINT #newAttachCommand
EXEC(#newAttachCommand)
DROP TABLE ##DBFileNames
You could just create a new database and then go to tasks, import data, and import all the data from the database you want to duplicate to the database you just created.
This program copies a database to the same server under a different name. I relied on examples given on this site with some improvements.
-- Copies a database to the same server
-- Copying the database is based on backing up the original database and restoring with a different name
DECLARE #sourceDb nvarchar(50);
DECLARE #destDb nvarchar(50);
DECLARE #backupTempDir nvarchar(200)
SET #sourceDb = N'Northwind' -- The name of the source database
SET #destDb = N'Northwind_copy' -- The name of the target database
SET #backupTempDir = N'c:\temp' -- The name of the temporary directory in which the temporary backup file will be saved
-- --------- ---
DECLARE #sourceDb_ROWS nvarchar(50);
DECLARE #sourceDb_LOG nvarchar(50);
DECLARE #backupPath nvarchar(400);
DECLARE #destMdf nvarchar(100);
DECLARE #destLdf nvarchar(100);
DECLARE #sqlServerDbFolder nvarchar(100);
Declare #Ret as int = -1
Declare #RetDescription nvarchar(200) = ''
-- Temporary backup file name
SET #backupPath = #backupTempDir+ '\TempDb_' + #sourceDb + '.bak'
-- Finds the physical location of the files on the disk
set #sqlServerDbFolder = (SELECT top(1) physical_name as dir
FROM sys.master_files where DB_NAME(database_id) = #sourceDb );
-- Clears the file name and leaves the directory name
set #sqlServerDbFolder = REVERSE(SUBSTRING(REVERSE(#sqlServerDbFolder), CHARINDEX('\', REVERSE(#sqlServerDbFolder)) + 1, LEN(#sqlServerDbFolder))) + '\'
-- Finds the logical name for the .mdf file
set #sourceDb_ROWS = (SELECT f.name LogicalName FROM sys.master_files f INNER JOIN sys.databases d ON d.database_id = f.database_id
where d.name = #sourceDb and f.type_desc = 'ROWS' )
-- Finds the logical name for the .ldf file
set #sourceDb_LOG = (SELECT f.name LogicalName FROM sys.master_files f INNER JOIN sys.databases d ON d.database_id = f.database_id
where d.name = #sourceDb and f.type_desc = 'LOG' )
-- Composes the names of the physical files for the new database
SET #destMdf = #sqlServerDbFolder + #destDb + N'.mdf'
SET #destLdf = #sqlServerDbFolder + #destDb + N'_log' + N'.ldf'
-- If the source name is the same as the target name does not perform the operation
if #sourceDb <> #destDb
begin
-- Checks if the target database already exists
IF Not EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = #destDb)
begin
-- Checks if the source database exists
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = #sourceDb) and (#sqlServerDbFolder is not null)
begin
-- Opens the permission to run xp_cmdshell
EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE WITH OVERRIDE
-- If the temporary backup directory does not exist it creates it
declare #md as nvarchar(100) = N'if not exist ' + #backupTempDir + N' md ' +#backupTempDir
exec xp_cmdshell #md, no_output
-- Creates a backup to the source database to the temporary file
BACKUP DATABASE #sourceDb TO DISK = #backupPath
-- Restores the database with a new name
RESTORE DATABASE #destDb FROM DISK = #backupPath
WITH REPLACE,
MOVE #sourceDb_ROWS TO #destMdf,
MOVE #sourceDb_LOG TO #destLdf
-- Deletes the temporary backup file
declare #del as varchar(100) = 'if exist ' + #backupPath +' del ' +#backupPath
exec xp_cmdshell #del , no_output
-- Close the permission to run xp_cmdshell
EXEC master.dbo.sp_configure 'xp_cmdshell', 0
RECONFIGURE WITH OVERRIDE
EXEC master.dbo.sp_configure 'show advanced options', 0
RECONFIGURE WITH OVERRIDE
set #ret = 1
set #RetDescription = 'The ' +#sourceDb + ' database was successfully copied to ' + #destDb
end
else
begin
set #RetDescription = 'The source database '''+ #sourceDb + ''' is not exists.'
set #ret = -3
end
end
else
begin
set #RetDescription = 'The target database '''+ #destDb + ''' already exists.'
set #ret = -4
end
end
else
begin
set #RetDescription = 'The target database ''' +#destDb + ''' and the source database '''+ #sourceDb + ''' have the same name.'
set #ret = -5
end
select #ret as Ret, #RetDescription as RetDescription
<!doctype html>
<head>
<title>Copy Database</title>
</head>
<body>
<?php
$servername = "localhost:xxxx";
$user1 = "user1";
$pw1 = "pw1";
$db1 = "db1";
$conn1 = new mysqli($servername,$user1,$pw1,$db1);
if($conn1->connect_error) {
die("Conn1 failed: " . $conn1->connect_error);
}
$user2 = "user2";
$pw2 = "pw2";
$db2 = "db2";
$conn2 = new mysqli($servername,$user2,$pw2,$db2);
if($conn2->connect_error) {
die("Conn2 failed: " . $conn2->connect_error);
}
$sqlDB1 = "SELECT * FROM table1";
$resultDB1 = $conn1->query($sqlDB1);
if($resultDB1->num_rows > 0) {
while($row = $resultDB1->fetch_assoc()) {
$sqlDB2 = "INSERT INTO table2 (col1, col2) VALUES ('" . $row["tableRow1"] . "','" . $row["tableRow2"] . "')";
$resultDB2 = $conn2->query($sqlDB2);
}
}else{
echo "0 results";
}
$conn1->close();
$conn2->close();
?>
</body>
If you are MS SQL 2014 and newer;
DBCC CLONEDATABASE (CurrentDBName, NewDBName)
GO
Details;

Resources