I've created a SQL agent job in our SQL Server 2008 that executes the following code:
BEGIN
SET NOCOUNT ON;
declare @database nchar(20);
DECLARE Database_Cursor CURSOR FOR
SELECT [name]
FROM master.dbo.sysdatabases
ORDER BY [name]
OPEN Database_Cursor;
FETCH NEXT FROM Database_Cursor INTO @database;
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC [dbo].[sp_BackupDatabase]
@databaseName = @database
FETCH NEXT FROM Database_Cursor INTO @database;
END;
CLOSE Database_Cursor;
DEALLOCATE Database_Cursor;
END
GO
Basically I retrieve a list of databases and execute a backup script for each database. This script works for most database, but some databases return an error:
Msg 911, Level 16, State 11, Line 1
Database 'appName_Configuration' does not exist. Make sure that the name is entered correctly.
This is correct, this database does not exist. We have several database named like this:
- appName_Configuration1
- appName_Configuration2
- ...
For some reason, the script doesn't pass the name including the number to the backup script. If I replace the @database variable with a static name (appName_Configuration1) it works just fine.
Also worth mentioning: there are a few other databases that have a number at the end, which work fine:
- appName_Microsoft1
- appName_Microsoft2
I suspect that the word "Configuration" may have something to do with it, but renaming the database is not an option for now. Can anyone help me finding a solution so that the name is passed to the stored procedure correctly ?
Kind regards, Mathew