



CREATE PROCEDURE DBO.ChangeObjectOwnerOnTables AS
SET NOCOUNT ON
/*
   NOTE: This SP has problems if logged in user does not have permission to change owner on the table.
*/

DECLARE
@TableName VARCHAR (80),
@Name VARCHAR (80)

DECLARE TableCursor CURSOR FOR

 SELECT sysobjects.Name AS 'TableName' FROM sysobjects WITH (NOLOCK) WHERE xtype ='U' AND user_name(uid) ='sysdba'

OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName

WHILE @@fetch_status = 0
BEGIN
  PRINT @TableName
  SET @name = 'sysdba.'+@TableName
  EXEC sp_changeobjectowner  @name, 'dbo'

  FETCH NEXT FROM TableCursor INTO @TableName
END

CLOSE TableCursor
DEALLOCATE TableCursor

