Restoring database from script - sql-server

I have created a small scrip to restore databases from backups. Once the script is run it displays
RESTORE DATABASE successfully processed 28818 pages in 1.568 seconds
(143.584 MB/sec).
I have more code to alter the database, alter a few views and sp too but I am getting the following error; User does not have permission to alter database 'GreyGoo', the database does not exist, or the database is not in a state that allows access checks.
I have noticed too that I cannot see the database in the object explorer
this is what I use to restore the DB from a backup
if DB exists set to a single user
if DB exist drop database
Ran the below script
RESTORE DATABASE GreyGoo FROM DISK = 'C:\Bkp\GreyGoo_backup_2020_03_02_180002_5403592.bak'
WITH
MOVE 'GreyGoo' TO 'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\GreyGoo.mdf',
MOVE 'GreyGoo_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\GreyGoo.ldf',
REPLACE;
set DB to multi-users and other properties
So what is the problem and how can I solve it, I am currently testing my code on SQL Server 2008
Thanks

During your restore process, you will need to ensure your
login / permissions are correct.
if another previous database with same name is not there, if so delete it. Perhaps from a previous attempt
You can also turn off recovery option like below, recovery is default
RESTORE FILELISTONLY
Sample -- MSDN
USE master;
GO
-- First determine the number and names of the files in the backup.
-- AdventureWorks2012_Backup is the name of the backup device.
RESTORE FILELISTONLY
FROM AdventureWorks2012_Backup;
-- Restore the files for MyAdvWorks.
RESTORE DATABASE MyAdvWorks
FROM AdventureWorks2012_Backup
WITH RECOVERY,
MOVE 'AdventureWorks2012_Data' TO 'D:\MyData\MyAdvWorks_Data.mdf',
MOVE 'AdventureWorks2012_Log' TO 'F:\MyLog\MyAdvWorks_Log.ldf';
GO

EXEC msdb.dbo.sp_delete_database_backuphistory #database_name = N'MYDB'
GO
USE [master]
GO
/* Database [MYDB] */
DROP DATABASE [MYDB]
GO
USE [master]
RESTORE DATABASE [MYDB] FROM DISK = N'E:\FTP\local_MYDB_01.bak' WITH FILE = 1, MOVE N'blank' TO N'E:\DBs\2019\MYDB.mdf', MOVE N'blank_log' TO N'E:\DBs\2019\MYDB.ldf', NOUNLOAD, STATS = 5
GO

Related

Restore .bak database file on SQL Server without using SSMS? [DataGrip]

Compared to SSMS, Datagrip doesn't really seem to have the "Restore Database" option, so apparently the only way is through script.
I tried the following:
RESTORE FILELISTONLY
FROM DISK = 'C:\Users\Marcelo\Downloads\Northwind.bak'
GO
RESTORE DATABASE Northwind
FROM DISK = 'C:\Users\Marcelo\Downloads\Northwind.bak'
WITH
MOVE 'Northwind' TO 'C:\Program Files\Microsoft SQL Server\MSSQL14.LENOBRAC\MSSQL\DATA\Northwind.mdf',
MOVE 'Northwind_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL14.LENOBRAC\MSSQL\DATA\Northwind_log.ldf',
GO
[2019-02-22 15:29:41] completed in 1 s 117 ms
The console shows that the task has been completed with no problems, however the database does not appear on the side explorer, I also cannot select any tables or do anything, I can only drop the database.
What could be the reason for this?
Just try,
USE [master]
RESTORE DATABASE Northwind
FROM DISK = N'C:\Users\Marcelo\Downloads\Northwind.bak'
WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
GO
Right click on the hostname / datasource name in the explorer panel
Go to database tools
you'll find an option called manage shown schemas , just tick the
mark next to databases you want to see

SQL-Server: Cannot be overwritten. It is being used by database

RESTORE DATABASE Tes
FROM DISK = '{7522204E-0256-47B3-9864-137D6D1FD449}6'
WITH MOVE 'Test' TO 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\Tes.mdf',
MOVE 'Test_log' TO 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\Tes.ldf'
The above restore command is working fine for new dbs, But few old databases it showing a errors like
The file 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\db1.mdf' cannot be overwritten. It is being used by database 'windb'.
Msg 3156, Level 16, State 4, Line 1
File 'windb' cannot be restored to 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\db1.mdf'. Use WITH MOVE to identify a valid location for the file
I found that only dbs having more than one ndf files having this issues ..
Any solutions this issue ?
Try to restore the database from the Restore Database Wizard. For that, in the Object Explorer, right-click on Databases -> select Restore Databases.... In the window that appears, set the necessary details for the Source and the Destination. And in the left pane, either select Options or Files, depending on the SSMS version you have.
In the table that appears, find the column Restore As. In each row, give a new file name, instead of the name that shows. For example, in your case it might be showing the file names as something similar to <Folder Path>\DATA\Tes.mdf and <Folder Path>\DATA\Tes.ldf. Give a different name other than Tes. (A name that does not already exist in the DATA folder).
Note: The file names do not have anything to do with the database name, but it will easy referencing later on to give a meaningful name, in case you have to backup or look for the files.
If you are trying to move your database to a new location, you should better detach the source database or take it offline first
I faced this issue when working on an MS SQL Server 2012.
I was trying to restore a database using the script below
USE master;
GO
ALTER DATABASE my_db SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
RESTORE DATABASE my_db.dev
FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\com.mydb.dev.bak'
WITH REPLACE,
STATS = 10,
RESTART,
MOVE 'com.mydb.dev' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.mydb.dev.mdf',
MOVE 'com.mydb.dev_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.mydb.dev_log.ldf'
GO
ALTER DATABASE com.mydb.dev SET MULTI_USER;
GO
And then this was throwing the error below when I tried executing the restore task:
The file 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.mydb.dev.mdf' cannot be overwritten. It is being used by database restdb
The file 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.mydb.dev_log.ldf' cannot be overwritten. It is being used by database restdb
Here's how I fixed it:
The issue was that the database backup file that I was using for this database restore operation had been previously used to restore data to a different database on the same server.
All I had to do was to change the Physical names of the Logical files to that of the database that I was to restore to. That is from com.mydb.dev.mdf to my_db.mdf and from com.mydb.dev_log.ldf to my_db_log.ldf:
USE master;
GO
ALTER DATABASE my_db SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
RESTORE DATABASE my_db
FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\com.mydb.dev.bak'
WITH REPLACE,
STATS = 10,
RESTART,
MOVE 'com.my_db.dev' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\my_db.mdf',
MOVE 'com.mydb.dev_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\my_db_log.ldf'
GO
ALTER DATABASE my_db SET MULTI_USER;
GO
And this time, the database restore task ran successfully.
That's all.
I hope this helps

Duplicate a Database in SQL Server 2014

I have a live database. I need to create a test database with all the data from the live one, but I've encoutneered an error. I've tried to create this new one by restoring from a "live database .bak", but an error message appears saying that could not do the process because the database is in use. Also, I don't know why but after that, the live database starts restoring and keep on it until getting stuck. It does not finish restoring and I have to force stop, deleting the live database and restoring from scratch. Checking the activity log it seems that it goes to restoring mode after failing to create a backup of the database log and that's what happens right after I try to create the new test database using the .bak.
What am I doing wrong? Is there another way to duplicate the live database to use it as a test one?
Note: I've done the process when nobody is using the DB or connected to it but no success.
You need to relocate/rename the Files. Just changing the Database name that you are restoring to may not be changing the Database File and Log File that SQL is trying to actually create when you perform the restore. Go to the Option page of the Restore Database and ensure the files are pointing to Valid locations and not trying to utilize the same files as the Live database.
--At First take ypur database backup and restore it as DatabaseName_Test and also change Datafiles As database _test.mdf and DatabaseName_test_log.ldf
--try to follow as below script
USE master
GO
BACKUP DATABASE [DatabaseName]
TO DISK = N'D:\SQLSERVER_Installation\setup\MSSQL12.SQLSERVER2014\MSSQL\Backup\DatabaseName.bak'
WITH COPY_ONLY, NOFORMAT, NOINIT,
NAME = N'DatabaseName-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10
GO
USE [master]
RESTORE DATABASE [DatabaseName_Test] FROM
DISK = N'D:\SQLSERVER_Installation\setup\MSSQL12.SQLSERVER2014\MSSQL\Backup\DatabaseName.bak'
WITH FILE = 2,
MOVE N'DatabaseName' TO N'D:\SQLSERVER_Installation\setup\MSSQL12.SQLSERVER2014\MSSQL\DATA\DatabaseName_test.mdf',
MOVE N'DatabaseName_log' TO N'D:\SQLSERVER_Installation\setup\MSSQL12.SQLSERVER2014\MSSQL\DATA\DatabaseName_test_log.ldf', NOUNLOAD, STATS = 5
GO

20GB SQL Server DataBase Backup resore

I have a SQL server data backup size is above 20 GB. My database is in E drive have enough space but my SQL software installed in C drive which has low memory. In this scenario how can i restore my backup to E drive.
You can restore database via SQL Server Management Studio.
While restoring , you can specify MDF and LDF file location to your E: drive.
To specify the new location of the database files, select the Files
page, and then click Relocate all files to folder. Provide a new
location for the Data file folder and Log file folder
THIS MIGHT HELP
steps :
ALTER DATABASE database_name SET OFFLINE;
Move the file or files to the new location.
For each file moved, run the following statement.
ALTER DATABASE database_name MODIFY FILE ( NAME = logical_name, FILENAME = 'new_path\os_file_name' );
Run the following statement.
ALTER DATABASE database_name SET ONLINE;
Verify the file change by running the following query.
SELECT name, physical_name AS CurrentLocation, state_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'<database_name>');
for more details go through url
USE MASTER;
GO
ALTER DATABASE DBName
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
-- Detach DB
EXEC MASTER.dbo.sp_detach_db #dbname = N'DBName'
GO
Step 1:Detach DB
USE MASTER;
GO
ALTER DATABASE DBName
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
-- Detach DB
EXEC MASTER.dbo.sp_detach_db #dbname = N'DBName'
GO
Step 2:
Now move the files from C drive to E drive. You can now reattach the files with E drive.

How do I make a copy of an SQL Server database and connect to it?

I'm working on vb.NET 2013 and SQL server 2008R2.
I have this situation to resolve:
I have a database "DB1". I want to create a copy of this database on the same SQL server with another name "DB2", and after make this database ready to connect.
How can I do this through code from Vb.NET?
Check out this tech net link for restore with new file names.
http://technet.microsoft.com/en-us/library/ms190447(v=sql.105).aspx.
Take a backup of database one (DB1).
Verify backup is valid.
Get correct logical and physical file names
Restore with a move command to create database two (DB2).
Change ownership to SA. Defaults to SQL login that creates the database
Here is a script that I tested on my laptop. Nice thing about backup is no need for down time. If you decide to take off line, copy data files, and create database for attach, down time is created.
-- Sample database
USE AdventureWorks2012;
GO
-- 1 - Make a backup
BACKUP DATABASE AdventureWorks2012
TO DISK = 'C:\mssql\backup\AdventureWorks2012.Bak'
WITH FORMAT,
MEDIANAME = 'SQLServerBackups',
NAME = 'Full Backup of AdventureWorks2012';
GO
-- Use master
USE master
GO
-- 2 - Is the backup valid
RESTORE VERIFYONLY
FROM DISK = 'C:\mssql\backup\AdventureWorks2012.Bak';
GO
-- 3 - Check the logical / physical file names
RESTORE FILELISTONLY
FROM DISK = 'C:\mssql\backup\AdventureWorks2012.Bak';
GO
-- 4 - Restore the files change the location and name
RESTORE DATABASE Adv2012
FROM DISK = 'C:\mssql\backup\AdventureWorks2012.Bak'
WITH RECOVERY,
MOVE 'AdventureWorks2012_Data' TO 'c:\mssql\data\MyAdvWorks_Data.mdf',
MOVE 'AdventureWorks2012_Log' TO 'c:\mssql\log\MyAdvWorks_Data.ldf';
GO
-- 5 - Switch owner to system admin
ALTER AUTHORIZATION ON DATABASE::Adv2012 TO SA;
GO

Resources