Solved

stored proc to backup a list of databases

Posted on 2014-01-30
4
212 Views
Last Modified: 2014-02-03
I would like to create a stored procedure that will take a list of databases which I will supply upon execution, that will run a full backup.  I'm having trouble with getting it to loop through my list.
0
Comment
Question by:navajo26354
4 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39822798
check this out

declare @T TABLE
(
dbNames VARCHAR(100)
,backupPath VARCHAR(1000)
)
INSERT INTO @T VALUES('DB1')
INSERT INTO @T VALUES('DB2')

INSERT INTO @T VALUES('DB3')
INSERT INTO @T VALUES('DB4')

DECLARE @DbName VARCHAR(100)
DECLARE @BackupPath VARCHAR(1000)
DECLARE @SQL VARCHAR(1000)
DECLARE T CURSOR
FOR 
SELECT dbNames FROM @T

OPEN T
FETCH T INTO @dbName,@BackupPath

WHILE @@FETCH_STATUS = 0 
BEGIN
SET @BackupPath = @BackupPath + @dbName + '.bak'
SET @SQL = 'del '+@BackupPath
exec xp_cmdshell @SQL
SET @SQL = 'backup database ' + @dbName + ' to disk = ' + '''' + @BackupPath + ''''
exec(@SQL)

FETCH T INTO @dbName,@BackupPath
END
CLOSE T
DEALLOCATE T

Open in new window

0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 39822810
try the solution mentioned here

http://ola.hallengren.com/
0
 
LVL 35

Expert Comment

by:David Todd
ID: 39822929
Hi,

Second Ola's script that Aneesh mentions above. As in why write your own when Ola's does nearly everything I can think of and then some!

Regards
  David

PS From someone who in the past has written their own scripts and restore jobs for test environments ...
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 39825635
I use a table to control rebuilds instead of passing in a list or using Ola's code.

That allows a much more customized approach WITHOUT constantly having to change code, such as calling Ola's procedures.  It also allows significant changes to the backup process to be made quickly and easily just by UPDATEing the table, again instead of having to write or alter code.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
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.

744 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

12 Experts available now in Live!

Get 1:1 Help Now