Solved

SQL Backup Scripts

Posted on 2013-10-23
2
250 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:Carla Romere
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Carla Romere
ID: 39595632
This was perfect.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

688 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