Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

TSQL to copy backups to a new location

$
0
0

Hello,

I am trying to select the most recent backup of a database and move it over to a new location. However I am unable to assign the top1 max to a variable. Here is my script:

DECLARE @cmd nvarchar(4000)
DECLARE @srcbkpLocation nvarchar(4000)
DECLARE @destbkpLocation nvarchar(4000)
DECLARE @bkpFileName nvarchar(4000)
set @destbkpLocation='\\ServerB\FULL\Backups\NameofDB\'
set @srcbkpLocation='E:\Backups\NameofDB\'

SELECT top 1 @bkpFileName= max(bs.backup_finish_date) as Mostrecent,
	bs.database_name AS DatabaseName, 
	bms.physical_device_name
FROM msdb.dbo.backupset bs
INNER JOIN msdb.dbo.backupmediafamily bms 
ON 	bs.media_set_id = bms.media_set_id
INNER JOIN master.dbo.sysdatabases s 
ON 	bs.database_name = s.name
WHERE 
	s.dbid IN (6) AND
	bs.type = 'D'
GROUP BY bs.database_name,bms.physical_device_name order by Mostrecent desc

SET @cmd = 'xcopy ' +@srcbkpLocation++@bkpFileName + ' ' + @destbkpLocation++@bkpFileName 
EXEC master.dbo.xp_cmdshell @cmd, NO_OUTPUT

can someone tell me where am I doing wrong?



Viewing all articles
Browse latest Browse all 23857

Trending Articles