MSSQL Fix Owner

 For whatever reason, it’s occasionally necessary to change the owner of tables in MSSQL.  In this example, we are changing the owner from “oldowner” to “dbo”:

declare tabcurs cursor
for
   select 'oldowner.' + name
   from sysobjects
   where xtype = 'u' OR xtype = 'v'

open tabcurs
declare @tname nvarchar(517)
fetch next from tabcurs into @tname
while @@fetch_status = 0
begin
   exec sp_changeobjectowner @tname, 'dbo'
   fetch next from tabcurs into @tname
end
close tabcurs
deallocate tabcurs