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

<<  June 2019  >>
MoTuWeThFrSaSu
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

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 2019

Sign in