I am using SQL Server 2008 R2
.
I had created an SQL Script
for my project's database i.e. used to create all the tables, constraints, views, stored procedures and functions with some minimal data for creating a fresh database.
But by mistake, I had ran it on the master
database. So that all of those stuff were created in master
database.
Now, I want to drop
all that user-defined objects
from master
database.
Is there any easy way to do it?
If you browse to the tables overview in the object browser and then select the object browser detail pane, you can sort on e.g. table create date and select all the tables you want to drop. check the checkbox "continue on error" and then hit your delete button.
To delete a database In Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance. Expand Databases, right-click the database to delete, and then click Delete.
I have just created this Script to create a Script to drop all User Defined Objects in you Master database just test it on Dev server before you execute it on Production server ..
SELECT
'DROP ' + CASE WHEN type = 'U' THEN 'TABLE '
WHEN type = 'P' THEN 'PROCEDURE '
WHEN type = 'FN'THEN 'FUNCTION '
WHEN type = 'V'THEN 'VIEW ' END
+ QUOTENAME(s.[name]) + '.' + QUOTENAME(o.[name]) + CHAR(10) + 'GO' + CHAR(10)
FROM master.sys.objects o
INNER JOIN master.sys.schemas s ON o.[schema_id] = s.[schema_id]
WHERE o.[is_ms_shipped] <> 1
AND o.[type] IN ('U','P','FN','V')
-- Results to Text --
Generate Script
DROP TABLE [dbo].[Test_table1]
GO
DROP PROCEDURE [hr].[usp_Test_Proc1]
GO
DROP VIEW [views].[vw_TestView_1]
GO
DROP PROCEDURE [dbo].[usp_Test_Proc2]
GO
DROP FUNCTION [dbo].[udf_Test_Function_GetList]
GO
Note
If the generated Script tries to delete a table that is referenced by other table via Foreign Key it will throw an error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With