Remove all objects from a SQL Schema - Cannot drop schema because it is being referenced by object

by ebarcza 9/1/2015 2:09:00 PM

IF you run into this error when trying to delete a schema(usually as a result of trying to delete a user that owns a schema)

"Cannot drop schema because it is being referenced by object"

 

First run this sql to generate the statements that will execute the move

SELECT 'ALTER SCHEMA dbo TRANSFER [' + SysSchemas.Name + '].[' + DbObjects.Name + '];'
FROM sys.Objects DbObjects
INNER JOIN sys.Schemas SysSchemas ON DbObjects.schema_id = SysSchemas.schema_id
WHERE SysSchemas.Name = 'upstreamgasmgmt'
AND (DbObjects.Type IN ('U', 'P', 'V'))

The previous sql statement will return a set of records like this

ALTER SCHEMA dbo TRANSFER [upstreamgasmgmt].[GetReportPoints];
ALTER SCHEMA dbo TRANSFER [upstreamgasmgmt].[mappingTest];
ALTER SCHEMA dbo TRANSFER [upstreamgasmgmt].[vwPhysicalZones];
ALTER SCHEMA dbo TRANSFER [upstreamgasmgmt].[PantoneStaging_MeasurementVolumes];
ALTER SCHEMA dbo TRANSFER [upstreamgasmgmt].[tbl_Volumes_PantonTest];
ALTER SCHEMA dbo TRANSFER [upstreamgasmgmt].[test];
ALTER SCHEMA dbo TRANSFER [upstreamgasmgmt].[ReportLAUFWellhead];
ALTER SCHEMA dbo TRANSFER [upstreamgasmgmt].[November204BidWeek];

 

Copy and paste that result set into a query window and run it.

 

Now try and delete the schema again!

Tags:

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

Name of author Author name
Something about me and what I do.

E-mail me Send mail

Calendar

<<  November 2017  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Recent comments

Tags

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2017

Sign in