Solved

SQL Backup Scripts

Posted on 2013-10-23
2
248 Views
Last Modified: 2013-10-23
I am going through all of our different sql servers and correcting some backup scripts that were failing. This is the script that I'm using as a single step in an SQL Agent Job:

DECLARE @FILENAME AS VARCHAR(255)
SELECT @FILENAME = N'D:\SQL Backups\AERT\AERT_Full_' + CAST(GETDATE() AS VARCHAR(20)) + '.bak'

BACKUP DATABASE [AERT] TO  DISK = @FILENAME WITH NOFORMAT, NOINIT,  NAME = N'AERT-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
GO
declare @backupSetId as int
select @backupSetId = position from msdb..backupset where database_name=N'AERT' and backup_set_id=(select max(backup_set_id) from msdb..backupset where database_name=N'AERT' )
if @backupSetId is null begin raiserror(N'Verify failed. Backup information for database ''AERT'' not found.', 16, 1) end
RESTORE VERIFYONLY FROM  DISK = @FILENAME WITH  FILE = @backupSetId,  NOUNLOAD,  NOREWIND
GO

Open in new window

This code is failing with this error: Msg 137, Level 15, State 2, Line 4
Must declare the variable '@FILENAME'.

This particular script will hopefully be used on SQL Server 2000.
0
Comment
Question by:Hers2keep
2 Comments
 
LVL 10

Accepted Solution

by:
PadawanDBA earned 500 total points
ID: 39595450
The GO command terminates the batch and the variable declaration/definition along with it.  Go ahead and try removing that.
0
 

Author Closing Comment

by:Hers2keep
ID: 39595632
This was perfect.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question