How to Change Schema of Mssql Tables, Stored Procedures, and Views All at the Same Time

Sometimes changing database users changes who the database owner is for a table from dbo.tablename to badschema.tablename. This will also affect any stored procedures created under the tables with the incorect schema.  If you need to change the schema for these tables back to the default dbo schema follow the steps below.

  1. Open Microsoft SQL Server Management Studio and log in.
  2. Click the New Query button.
  3. Paste the following script into the New Query box changing oldschema to the name of the current schema:

SELECT ‘ALTER SCHEMA dbo TRANSFER ‘ + s.Name + ‘.[‘ + o.Name + ‘];’
FROM sys.Objects o
INNER JOIN sys.Schemas s on o.schema_id = s.schema_id
WHERE s.Name = ‘gthompso’
And (o.Type = ‘U’ Or o.Type = ‘P’ Or o.Type = ‘V’)