SQL Server Script to Change DB Owner to SA for all the user DBs in the Instance.
--Script:17 Change DB Owner to SA for all the User DBs in the Instance.
SET ANSI_NULLS ON;
SET ARITHABORT ON;
SET QUOTED_IDENTIFIER ON;
SET NOCOUNT ON;
Declare @dbname varchar(max)
DECLARE @command nvarchar(4000);
DECLARE db_names CURSOR FOR SELECT name FROM sys.databases
where name not in ('master', 'model', 'msdb', 'tempdb');
open db_names;
fetch next from db_names into @dbname;
while @@FETCH_STATUS = 0
Begin
Set @command = N'Use [' + @dbname +'];
EXEC dbo.sp_changedbowner @loginame = N''sa'', @map = false';
fetch next from db_names into @dbname
execute (@command);
--print N'Executed: ' + @command
End
close db_names;
deallocate db_names;
Go
--Verfiy DB owner changed to sa
--sp_helpdb
Comentarios