SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Database backup script


Database backup script

Author
Message
SQL_Loser
SQL_Loser
Valued Member
Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)

Group: General Forum Members
Points: 53 Visits: 141
I have been tasked with creating a sql table that contains database backups.
The idea is to have all databases listed in rows that can be read by a script that will execute a backup for each row. I know SQL Server agent can do this but there are reasons that this needs to be in this format. In the following script I have created a table and populated it with an adventureworks database. The table is built so that the fields can be concatenated to form a Backupstatement(With some text added in).

Use Adventureworks;

If object_ID('Adventureworks.dbo.Db_Backups') is not null
Drop table Adventureworks.dbo.DB_Backups

Create Table Adventureworks.dbo.DB_Backups
(
database_Name varchar(30) Not null,
Backup_Freq varchar(10) Not null,
Backup_Type varChar(10) Not null,
Backup_To Varchar(300) Not null,
Format char(10) Not null,
Init_NoInit char(30) Not Null,
Backup_name Varchar(50) Not null,
Skip_NoSkip Char(6) Not Null,
rewind_Norewind Char(8) Not Null,
Nounload_unload Char(8) Not Null,
Stat_report Char(3) Not null)
GO

Insert into Adventureworks.dbo.DB_Backups
values
(
'Adventureworks',
'Daily',
'FUll',
'''C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\Adventureworks_backup_2012_08_27_093633_3380858.bak''',
'NOFORMAT',
'NOINIT',
'''Adventureworks-Full Database Backup''',
'SKIP',
'NOREWIND',
'NOUNLOAD',
100)
Go

Select *
FROM Adventureworks.dbo.DB_Backups

Declare @StrSql varchar(1000)

Set @strsql =
'Backup Database' + ' ' + (Select Database_Name From Adventureworks.dbo.Db_Backups)
+ ' ' + 'To Disk' + ' ' + '=' + ' ' + (Select Backup_to From Adventureworks.dbo.Db_Backups)
+ ' ' + (Select Format From Adventureworks.dbo.Db_Backups) + ','
+ ' ' + (Select Init_NoInit From Adventureworks.dbo.db_Backups) + ','
+ ' ' + 'Name' + ' ' + '=' + ' ' + (Select Backup_name From Adventureworks.dbo.db_Backups) + ','
+ ' ' + (Select Skip_Noskip From Adventureworks.dbo.db_backups) + ','
+ ' ' + (Select rewind_Norewind From Adventureworks.dbo.db_Backups) + ','
+ ' ' + (Select Nounload_unload From Adventureworks.dbo.db_Backups) + ','
+ ' ' + 'Stats =' + ' ' + ' ' + (Select Stat_report From Adventureworks.dbo.db_backups)

Exec @strsql
GO


I am not too experienced with stored procedures and don't know it that is the way to go. I also tried making one column that contained a complete backup statement, but when I tried to execute the command on that column I got the same results.

Any help is appreciated or alternative suggestions,

Thanks,

GH
David Betteridge
David Betteridge
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1071 Visits: 1822
As it's currently written your sql will only work if you have a single database listed in your backups table.

I think I would use a cursor to loop down your table a row at a time.
For each row then build up the sql statement (similar to your sql)
and then exec it

You need to think about how errors will be handled. What happens if the first database in the list can't be backed up. Does the script continue, how will you be alerted?


What's the error message you are currently seeing?
David Betteridge
David Betteridge
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1071 Visits: 1822
two things wrong with the sql. You need to add + ' WITH ' + to the second line. And the exec command needs brackets.



Declare @StrSql varchar(1000)

Set @strsql =
'Backup Database' + ' ' + (Select Database_Name From dbo.Db_Backups)
+ ' ' + 'To Disk' + ' ' + '=' + ' ' + (Select Backup_to From dbo.Db_Backups) + ' WITH ' +
+ ' ' + (Select Format From dbo.Db_Backups) + ','
+ ' ' + (Select Init_NoInit From dbo.db_Backups) + ','
+ ' ' + 'Name' + ' ' + '=' + ' ' + (Select Backup_name From dbo.db_Backups) + ','
+ ' ' + (Select Skip_Noskip From dbo.db_backups) + ','
+ ' ' + (Select rewind_Norewind From dbo.db_Backups) + ','
+ ' ' + (Select Nounload_unload From dbo.db_Backups) + ','
+ ' ' + 'Stats =' + ' ' + ' ' + (Select Stat_report From dbo.db_backups)

print @strsql
Exec (@strsql)
GO
SQL_Loser
SQL_Loser
Valued Member
Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)

Group: General Forum Members
Points: 53 Visits: 141
I have left work but I recall the message stating that the database name was not valid or something similar, I'll check it out tomorrow. Sometimes I was getting a syntax error too regarding an apostrophe. I think that may have been related to the "with" that the other post pointed out. I will check it out tomorrow, thanks for your help and the tips about the errors!

GH
SQL_Loser
SQL_Loser
Valued Member
Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)

Group: General Forum Members
Points: 53 Visits: 141
Well, the statement executes now that I made the syntax changes, thanks! No I have to deal with the log and cursor issues. Crazy
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search