Solved

stored proc to backup a list of databases

Posted on 2014-01-30
4
224 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
[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
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:Scott Pletcher
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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 ?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

630 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