<?xml version="1.0" encoding="UTF-8"?>
<code>
  <code>## SQL Statement [sql]
Use Master

Declare @DatabaseName sysname
Declare @SQLCommand varchar(1024)
Declare curDBName Cursor For
Select [Name] From Master..Sysdatabases
Where [Name] Not In ('tempdb')

Open curDBName
Fetch curDBName Into @DatabaseName

While (@@fetch_status = 0)

Begin
        if databasepropertyex (@DatabaseName,'Status') = 'online'
        Begin
                Select @SQLCommand = 'Backup Database ' + @DatabaseName + 
                ' To Disk = ''D:\Backups\Databases\' + @DatabaseName + '.bak'' With Format'
                execute (@SQLCommand)
        End
        Fetch curDBName Into @DatabaseName
End

Close curDBName
Deallocate curDBName</code>
  <comment>I've recently blogged about this script and it seems to work very well. Does anyone has any improvements/suggestions or is it nearly perfect?</comment>
  <created-at type="datetime">2008-09-19T15:19:54+00:00</created-at>
  <id type="integer">497</id>
  <language>C#</language>
  <permalink>backup-all-ms-sql-server-databases</permalink>
  <refactors-count type="integer">2</refactors-count>
  <title>Backup All MS SQL Server Databases</title>
  <trackback-url></trackback-url>
  <updated-at type="datetime">2009-03-05T15:11:26+00:00</updated-at>
  <user-id type="integer">663</user-id>
  <refactors type="array">
    <refactor>
      <code>Use Master

Declare @ToExecute VarChar(8000)

Select @ToExecute = Coalesce (@ToExecute + 'Backup Database ' + [Name] + ' To Disk = ''D:\Backups\Databases\' + [Name] 	+ '.bak'' With Format;' + char(13),'')
From Master..Sysdatabases
Where [Name] Not In ('tempdb') And databasepropertyex ([Name],'Status') = 'online'

--Print @ToExecute
Exec (@ToExecute)</code>
      <code-id type="integer">497</code-id>
      <comment>Hi GateKiller

If you don't have many databases, lets say less than 75, the code below would do the job without cursor. (In general, cursor are not a good way to do thing). If you have too many databases, your script is good, I'll just move the condition in your if statement to the query that the cursor is made of as in the exemple below.

Hope you'll like it !</comment>
      <created-at type="datetime">2008-09-19T23:55:56+00:00</created-at>
      <id type="integer">24687</id>
      <language>C#</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Backup All MS SQL Server Databases</title>
      <user-id type="integer">1025</user-id>
      <user-name>Moonshield</user-name>
      <user-website>www.analystik.ca</user-website>
    </refactor>
    <refactor>
      <code></code>
      <code-id type="integer">497</code-id>
      <comment>@Moodshield: Love it! It's a very simple solution that will suit my server which only has about a dozen databases.

Many Thanks,
Stephen</comment>
      <created-at type="datetime">2008-09-22T11:59:22+00:00</created-at>
      <id type="integer">28448</id>
      <language>C#</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Backup All MS SQL Server Databases</title>
      <user-id type="integer">663</user-id>
      <user-name>GateKiller</user-name>
      <user-website>http://gatekiller.co.uk</user-website>
    </refactor>
  </refactors>
</code>
