Blog Post

SQL server change single user mode or backup or recovery model of all database sql script

Advertisements

Following is the DBA script which generally used for most cases the action needed for all the databases like migration, upgrade etc.

Change the database recovery model and set to single_user mode.

Backup of all databases.

-- Dynamic SQL
select 'alter database ['+name+'] set single_user with rollback immediate' , * from sys.databases
where database_id not in (1,2,3,4)-- exclude system DBs
select 'alter database ['+name+'] set multi_usere' , * from sys.databases
where database_id not in (1,2,3,4)-- exclude system DBs
select 'alter database ['+name+'] set recovery simple' , * from sys.databases
where database_id not in (1,2,3,4)-- exclude system DBs
select 'backup database ['+name+'] to disk = ''B:\SQL_backup\'+name+'_full.bak''' , * from sys.databases
where database_id not in (1,2,3,4)-- exclude system DBs

 

We can write anything in the single quote and the separator is + symbol and can use any system objects like sys.databases and sys.objects.

This will be helpful for junior DBAs.

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating