Mehedi Amin

-------------------DB Owner SA

IF (SELECT COUNT(*) FROM sys.databases WHERE owner_sid <> 0x01) <> 0
	BEGIN
		EXEC sp_MSForEachDB '
			USE [?]
			IF DB_ID() > 4
				BEGIN
					ALTER AUTHORIZATION ON DATABASE::[?] TO sa
				END
			'
END

--Another One:

 SELECT 'ALTER AUTHORIZATION ON DATABASE::' + QUOTENAME(name) + ' TO [SA];' 
 from sys.databases
     where name not in ('master', 'model', 'tempdb')

-----------------------------Compatibility_level

USE [master]
GO
SELECT 'ALTER DATABASE ['+name+'] SET COMPATIBILITY_LEVEL = 150' from sys.databases where database_id > 4




--------Change Recovery Model

-- Variables to be used in the script
DECLARE @name VARCHAR(50) -- the name of the database
DECLARE @strSQL nvarchar(150) -- the SQL that will be executed against each database
 
USE Master
 
-- Declare a cursor that will return all user databases
DECLARE db_cursor CURSOR FOR
SELECT name
FROM master.dbo.sysdatabases
WHERE name  NOT IN ('master','model','msdb','tempdb')
 
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name -- Bring the first database name into the @name variable
 
WHILE @@FETCH_STATUS = 0 -- loop through each of the databases, until no records left
BEGIN
-- Create a SQL statement that will be executed
SET @strSQL = 'ALTER DATABASE [' + @name + '] SET RECOVERY SIMPLE' -- Concatenate the SQL statement with the database name variable
EXEC SP_EXECUTESQL @strSQL -- run the statement
 
FETCH NEXT FROM db_cursor INTO @name -- Return the database
END
 
CLOSE db_cursor -- Without closing & deallocating the cursor, running the same code will generate a failure
DEALLOCATE db_cursor


--Change Schema

USE master
GO
DECLARE @LoginName varchar(256)
SET @LoginName ='DataAggLinkServerAdmin'

SELECT 'USE [' + Name + ']'
+ ';'
+ 'CREATE USER [' + @LoginName + '] FOR LOGIN [' + @LoginName + '] WITH DEFAULT_SCHEMA =dbo'
+ ';'
+ 'EXEC sp_addrolemember ''db_owner'', '''+ @LoginName + ''''
AS ScriptToExecute

FROM sys.databases 
WHERE name NOT IN ('Master','tempdb','model','msdb') -- Avoid System Databases
 AND (state_desc ='ONLINE') -- Avoid Offline Databases
 AND (source_database_id Is Null) -- Avoid Database Snapshot
ORDER BY Name