Tip

Moving entire SQL Server user databases

Here are several procedures that you can use to "move" all of your user databases from one SQL Server to another with ease.

1. The sp_backup_databases procedure gets a backup dir destination directory parameter and backs up a complete database backup to all user databases to this directory. The directory is assumed to exist. The procedure should reside in the master database of the SQL Server where all user databases exist.

CREATE proc sp_backup_databases @backupDir varchar(50(
as
declare @dbname sysname
declare @bckstmt varchar(100)
begin 
  create table #userdbs (name sysname(
  insert into #userdbs select name from sysdatabases where dbid > 6
  declare cdb cursor for select name from #userdbs 
  open cdb
  fetch cdb into @dbname
  while @@fetch_status = 0 
  begin 
       set @bckstmt = 'BackUp Database ' + @dbname + ' to ' +
                      'Disk = ' + char(39) + @backupDir + '' + 
                      rtrim(ltrim(@dbname)) + '.bak' + char(39)
       exec (@bckstmt)
       fetch cdb into @dbname
  end 
  close cdb
  deallocate cdb
  drop table #userdbs
end
GO
Here is an example:
 
exec sp_backup_databases 'c:SQLbackup'

2. The sp_restore_database procedure gets two parameters: location of database files and location of backup files directories. The procedure should reside in the master database of the SQL Server you want the databases to be restored to. If location is null, then files are restored to the original

    Requires Free Membership to View

position listed in the backup header. (as shown using restore filelistOnly)

Here is the code:

CREATE proc sp_restore_databases @loc varchar(100) =null, @backupdir varchar(100(
                                                          
as
declare @dbname   sysname
declare @dbshort  sysname
declare @line     varchar(100(
declare @reststmt varchar(600)
declare @ntstmt   varchar(100(
declare @datafilename sysname 
declare @logfilename sysname
declare @i        int
declare @pos1     int
begin

  set nocount on

 create table #listfile 
 ) Logical_name sysname,
   phisc_name sysname,
   type char(1), 
    fg varchar(30( ,
   siz bigint,
   maxsize bigint)

 create table #userdbs (line varchar(100))

  set @ntstmt = 'xp_cmdshell ' + char(39) + 'dir ' + @backupdir+ char(39)
  insert into #userdbs exec (@ntstmt)
  declare cdb cursor for select line from #userdbs 
      where upper(line) like '%BAK%'

  open cdb
  fetch cdb into @line
  while @@fetch_status = 0 
  begin 
       set @pos1 = CharIndex('.bak' ,@line,1)
       set @i = @pos1
       while substring (@line,@i,1) <> ' ' and @i > 1
       begin 
             set @i = @i - 1       
       end 
   
       set @dbname = substring (@line,@i+1,@pos1+3-@i)
       set @dbshort = rtrim(ltrim(replace(@dbname,'.bak','')))
      
       truncate table #listfile
       insert into #listfile    exec ('RESTORE filelistonly from disk=' + '''' + @backupdir + '' + @dbname  + '''')

       select @datafilename = Logical_name from #listfile where type = 'D'
       select @logfilename = Logical_name from #listfile where type = 'L'

       set @reststmt = 'restore Database ' + @dbshort + ' from ' +
                       'Disk = ' + char(39) +@backupdir + '' + 
                        rtrim(ltrim(@dbname)) + char(39)

       if Isnull (@loc,'*') <> '*' 
       begin 
          set @reststmt = @reststmt + ' with move ' + char(39) + @datafilename + char(39) +
              ' to ' + char(39) + @loc + '' + @datafilename + '.mdf' + char(39) + 
              ', move ' + char(39) + @logfilename + char(39) + ' to ' + char(39) + 
              @loc + ''  + @logfilename + '.ldf' + char(39)
       end
       print @reststmt
       exec (@reststmt)
       fetch cdb into @line
  end 
  close cdb
  deallocate cdb
  drop table #userdbs
  drop table #listfile
end
go
Example:
EXEC sp_restore_databases 'c:data','c:sqlBackup'

For More Information

  • Feedback: E-mail the editor with your thoughts about this tip.
  • More tips: Hundreds of free SQL Server tips and scripts.
  • Tip contest: Have a SQL Server tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
  • Ask the Experts: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
  • Forums: Ask your technical SQL Server questions--or help out your peers by answering them--in our active forums.
  • Best Web Links: SQL Server tips, tutorials, and scripts from around the Web.

This was first published in May 2003

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.