Manchester Web Design & SEO Specialists


SQL Server 2008 Web Maintenance Plans

• posted by Editor on Friday, 27 November 2009 10:52
If you're trying to setup a Maintenance plan in SQL Server 2008 Web Edition then read this article! It's not possible because SQL Server Integration Services does not come with Workgroup or Web Editions of SQL Server 2008.

If you naively setup a plan and try to execute it you will see an error message in the event logs stating:

"SQL Server Scheduled Job 'Web Vitality.Subplan_1' (0x0A776D635B886345B458F02C9D9877A1) - Status: Failed - Invoked on: 2009-11-27 08:28:55 - Message: The job failed. The Job was invoked by User XXXXXX\Administrator. The last step to run was step 1 (Subplan_1)."

However, it is possible to create SQL Agent Jobs with T-SQL scripts (Transact-SQL). For example, to add a job which will backup all databases, open the SQL Server Management Studio, right click on the SQL Server Agent and select 'New' then 'Job' and give the job a suitable name. Select the 'Steps' option and add a new step with the following T-SQL script (this will create a directory for each database in a folder located at C:\SQL_Backups):

DECLARE @basePath varchar(300);
SET @basePath = N'C:\SQL_Backups\';

DECLARE @db sysname;
DECLARE user_db_cursor CURSOR FOR
SELECT name FROM sys.databases WHERE name NOT IN ('master', 'model', 'msdb', 'tempdb')
OPEN user_db_cursor
FETCH NEXT FROM user_db_cursor
INTO @db

WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @backupPath varchar(1000);
SET @backupPath = @basePath + @db + '\';

DECLARE @backupName varchar(1000);
DECLARE @time datetime2;
SET @time = SYSDATETIME();
SET @backupName = @db + '_backup_' + REPLACE(REPLACE(REPLACE(CONVERT(varchar, @time, 20),'-','_'),':',''),' ','_')
+ '_' + CAST(DATEPART(NANOSECOND, @time)/100 as varchar)

--Create sub-directory
EXECUTE master.dbo.xp_create_subdir @backupPath

DECLARE @backupFileName varchar(1000);
SET @backupFileName = @backupPath + @backupName + '.bak';

--Backup database
BACKUP DATABASE @db TO
DISK=@backupFileName
WITH RETAINDAYS=14, NOFORMAT, NOINIT, NAME=@backupName,
SKIP, REWIND, NOUNLOAD, STATS=10

FETCH NEXT FROM user_db_cursor
INTO @db

END
CLOSE user_db_cursor
DEALLOCATE user_db_cursor

Labels: , ,