top of page

SQL Server Change DB Owner to SA

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


47 views0 comments

Recent Posts

See All

SQL Server Profiler Trace

--Import multiple trace file in sql Table. --All the rollover files will be automatically imported --provide number tracefile to capture...

Comments


bottom of page