Change all object owners

This tip shows you how to change the object owner.

This batch job changes the object owner where the uid = UseNAme:

SELECT * from sysobjects where uid = user_id('UseNAme')
declare @Return int
exec @Return = sp_configure 'allow updates', '1'
SELECT @Return as 'Returned Code'
GO
reconfigure WITH OVERRIDE
GO
DECLARE @Rows int, @Error int
BEGIN TRANSACTION
update sysobjects set uid = user_id('dbo') where uid = user_id('UseNAme')
SELECT @Error = @@Error, @Rows = @@RowCount
SELECT @Rows as '#Rows'
IF @Rows > 0
 BEGIN	
  SELECT  @Rows AS '#Rows'
  COMMIT TRANSACTION
 END
else 
 BEGIN
  SELECT @Error AS 'Error #'
  ROLLBACK TRANSACTION
 END

exec sp_configure 'allow updates', '0'
reconfigure WITH OVERRIDE
go

For More Information


This was last published in February 2005

Dig Deeper on SQL Server Database Modeling and Design

PRO+

Content

Find more PRO+ content and other member only offers, here.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchBusinessAnalytics

SearchDataCenter

SearchDataManagement

SearchAWS

SearchOracle

SearchContentManagement

SearchWindowsServer

Close