Sql to get most recent full backup file for a specific database in sql server

后端 未结 4 1106
长发绾君心
长发绾君心 2021-02-19 08:03

I need an sql command (dbcc or extended stored procedure?) or something that can be called from a sql server stored procedure to get the most recent full backup file available f

4条回答
  •  梦谈多话
    2021-02-19 08:30

    declare @backupfile as Varchar(255)
    
    SELECT TOP 1 @backupfile=mf.physical_device_name from msdb..backupset bk      
    join msdb..backupmediafamily mf on bk.media_set_id = mf.media_set_id   
    where database_name=N'sourcedatabasename' and bk.type='D' order by  
    backup_set_id desc
    
    ALTER DATABASE [databasename] SET  SINGLE_USER  WITH ROLLBACK IMMEDIATE 
    
    RESTORE DATABASE databasename
    FROM DISK = @backupfile
    WITH MOVE 'datafile' TO 'databasefilepath',
    MOVE 'logfilename' TO 'logfilepath', REPLACE
    
    ALTER DATABASE [databasename] SET  MULTI_USER  WITH ROLLBACK IMMEDIATE
    

提交回复
热议问题