Monday, March 26, 2012

How to change owner for all tables in a database.

I am trying to change owner of all tables in a database. How can I do
it?(alexkuzn@.gmail.com) writes:

Quote:

Originally Posted by

I am trying to change owner of all tables in a database. How can I do
it?


DECLARE @.tbl sysname
DECLARE tblcur INSENSITIVE CURSOR FOR
SELECT name FROM sysobjects
WHERE xtype = 'U' AND uid = user_id('oldowner')
OPEN tblcur
WHILE 1 = 1
BEGIN
FETCH tblcur INTO @.tbl
IF @.@.fetch_status <0
BREAK

EXEC sp_changeobjectonwer @.tbl, 'oldowner', 'newowner'
END
DEALLOCATE tblcur

The above is untested, and you may have to look up details in Books Online.

Furthermore, I'm assuming SQL 2000. On SQL 2005, the preferred solution is
different. In fact, odds are good that on SQL 2005 you would not need to do
this at all, since schema and onwer are separated. All depending on why you
want to change the owner, that is.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

No comments:

Post a Comment