Quantcast
Channel: SQLServerCentral » SQL Server 2016 » SQL Server 2016 - Administration » Latest topics
Viewing all articles
Browse latest Browse all 2648

Is this safe?

$
0
0
Hi everyone, I'm just wondering is this script that I wrote safe? Or could it be improved? I want to restore an existing backedup database - to a new name and location. I then want to query a relevent tabel in it to make sure it successfult restored. Then drop the restored database. Any suggestions/comments appreciated:Here is what I have so far:--1. Get the most recent backup of the datbase in questionDECLARE @physical_device_name varchar(1000)SELECT @physical_device_name = physical_device_name FROM msdb.dbo.backupmediafamilyWHERE media_set_id =( SELECT TOP 1 media_set_id FROM msdb.dbo.backupset WHERE database_name='My_Database' AND type='D' --D means full-backup type ORDER BY backup_start_date DESC)--2. Restore to a new name and fiel locationRESTORE DATABASE My_Database_NEW_NAMEFROM DISK = @physical_device_nameWITH MOVE 'TestRestoreDatabaseData' TO 'D:\SQL2014\Data\My_Database_NEW_NAME.mdf',MOVE 'TestRestoreDatabaseLog' TO 'E:\SQL2014\Logs\My_Database_NEW_NAME.ldf',REPLACE--3. Query a table in the resoted databseselect top 1 * from My_Database_NEW_NAME.SomeTable--4. Email with relevent messageEXEC msdb.dbo.sp_send_dbmail @recipients = 'a@b.com', @body = 'The stored procedure finished successfully.', @subject = 'Automated Success Message' ; --5. Remove the restored databaseDROP DATABASE My_Database_NEW_NAME;

Viewing all articles
Browse latest Browse all 2648

Latest Images

Trending Articles



Latest Images