Step1: Create an VIEW for generating the offline DBs command for selected DBs
create view DBOfflineView as
select 'alter database'+ ' '+name+' '+'set offline with rollback immediate' as command
from sys.databases
where name not in ('master','model','msdb','tempdb')
--Note: Here you can as per your requirement.For alter the Dbs to simple use,
select 'alter database'+ ' '+name+' '+'set recovery simple' as command
from sys.databases
where name not in ('master','model','msdb','tempdb')
Step2: Select from the View to see the output first
select Command from DBOfflineView
Step3: Create cursor for executing the Command.
DECLARE @OfflineCommand VARCHAR(500) --set offline command
DECLARE db_cursor CURSOR FOR
select Command from DBOfflineView
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @OfflineCommand
WHILE @@FETCH_STATUS = 0
BEGIN
exec(@OfflineCommand)
FETCH NEXT FROM db_cursor INTO @OfflineCommand
END
CLOSE db_cursor
DEALLOCATE db_cursor
/*
Note:
This is how you can put the selected databases offline.
Like this,you can make your own code as per requirement.