Solved

SQL Backup Scripts

Posted on 2013-10-23
2
240 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
Comment Utility
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
Comment Utility
This was perfect.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now