DECLARE @sql nvarchar(2000)
DECLARE @id int
DECLARE @temp TABLE (id int IDENTITY (1,1), command nvarchar(1000) NULL, done bit NULL)
INSERT @temp(command)
SELECT
'BACKUP DATABASE '
+ QUOTENAME(name)
+ ' TO DISK = '''
+ 'C:\SQLBackups\' + name
+ '_'
+ CONVERT(varchar,GETDATE(),112)
+ CONVERT(varchar,DATEPART(hour,GETDATE()))
+ CONVERT(varchar,DATEPART(minute,GETDATE()))
+ '.BAK'' WITH INIT'
FROM master.sys.databases
WHERE name NOT IN ('tempdb','pubs','northwind')
AND DATABASEPROPERTYEX (name,'status') = 'ONLINE'
WHILE EXISTS (SELECT * FROM @temp WHERE DONE IS NULL)
BEGIN
SELECT @id = (SELECT MIN(id) FROM @temp WHERE done IS NULL)
SELECT @sql = command FROM @temp WHERE id = @id
--SELECT @sql
EXEC (@sql)
UPDATE @temp
SET done = 1 WHERE id = @id
END
dumping ground for code that might be useful sometime, somewhere. Beware the formatting mangle of the blog post...
Sunday, July 20, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment