0

I am trying to do a Full Copy Only database backup from a linked server to drive E on another servers SAN. Not finding any help. The linked server SAPRS02 works and changed the RPC options but still doesn't work.

Any help is appreciated.

JJ

DECLARE @DateStr nvarchar(500), @ArchivePath nvarchar(500),
        @FileName nvarchar(500), @DestFileName nvarchar(500),
        @dbName nvarchar(255), @SvrName nvarchar(200),
        @RADBACKUP nvarchar(MAX)

SET @dbName = 'MYDB' 
SET @ArchivePath = 'E:\SQLBackups\MyDIR\'
SET @SvrName = REPLACE(@@SERVERNAME,'\','-')
SET @DateStr = CONVERT(NVARCHAR(20),GETDATE(),120)
SET @DateStr =  REPLACE(REPLACE(REPLACE(REPLACE(@DateStr,'',''),':',''),'-','_'),' ','')
SET @FileName = @SvrName + '_' + @DBName + '_CopyOnly_backup_' + @DateStr + '.bak'
SET @DestFileName = @ArchivePath + @FileName


SET @RADBACKUP = 'BACKUP DATABASE @dbName
TO DISK = @DestFileName
WITH COMPRESSION
COPY_ONLY, 
CHECKSUM, COMPRESSION,
NAME = @FileName, SKIP, STATS = 10'

--SELECT * FROM @RADBACKUP

EXECUTE SAPRS02.master.dbo.sp_executesql @RADBACKUP

ERROR is:

Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "@dbName".

2
  • Msg 137, Level 15, State 2, Line 1 Must declare the scalar variable "@dbName". Commented Oct 21, 2016 at 19:45
  • 1
    When you execute the string in @RADBACKUP, there is no scope on any of the variables in there. Either concatenate them as strings or use the parameter feature of sp_execute_sql. By concatenate as strings, I mean do this: SET @RADBACKUP = 'BACKUP DATABASE ' + @dbName + ' ' + Commented Oct 22, 2016 at 4:43

1 Answer 1

1
SET @RADBACKUP = 'BACKUP DATABASE ' + QUOTENAME(@dbName) + '
TO DISK = ''' + @DestFileName + '''
WITH COMPRESSION
COPY_ONLY, 
CHECKSUM, COMPRESSION,
NAME = ''' + @FileName + ''', SKIP, STATS = 10';
Sign up to request clarification or add additional context in comments.

5 Comments

Now I get this. Thanks
Incorrec6 syntax near 'Copy_Only'.
I was just missing a comma. Now, for some reason it trying to put the backup on H: drive. I don't have a H drive. Hmm. Still researching.
Msg 3013, Level 16, State 1, Line 1 BACKUP DATABASE is terminating abnormally. Msg 262, Level 14, State 1, Line 1 BACKUP DATABASE permission denied in database 'RAD'.
I worked around by loaded the data in a temp table and them using SSMS, exported to the other server, but still need to create a SSIS package to perform this on timely manner, at call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.