Sometimes when we import database tables from external server to our server, schema names are mixed up, like some tables show up as A.Table1 and other as B.Table2

We want that all should be A.table1 and A.Table2, below script can help in this:

DECLARE tabcurs CURSOR FOR

SELECT ‘B.’ + [name]

FROM sysobjects      WHERE xtype = ‘u’

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