Does anyone have a script that will delete all non-system tables/procs/views from a database?
I created some views, procs and tables which I need to clean up and doing them individually is too cumbersome.
SQL Server have some built-in schema, for example : dbo, guest, sys, and INFORMATION_SCHEMA which cannot be deleted. Syntax : DROP SCHEMA [IF EXISTS] schema_name; Note : Delete all objects from the schema before dropping the schema.
Right-click a database in SQL Server Object Explorer, and select Delete. Accept all the default settings in the Delete Database dialog, and click OK.
DROP is used to delete a whole database or just a table. The DROP statement destroys the objects like an existing database, table, index, or view. A DROP statement in SQL removes a component from a relational database management system (RDBMS).
You could always query your system catalog views and have it generate the necessary DROP statements:
SELECT 'DROP PROCEDURE [' + SCHEMA_NAME(schema_id) + '].[' + pr.NAME +']'
FROM sys.procedures pr
WHERE pr.is_ms_shipped = 0
UNION
SELECT 'DROP VIEW [' + SCHEMA_NAME(schema_id) + '].[' + v.NAME + ']'
FROM sys.views v
WHERE v.is_ms_shipped = 0
UNION
SELECT 'ALTER TABLE [' + SCHEMA_NAME(schema_id) + '].[' + OBJECT_NAME(fk.parent_object_ID) + '] DROP CONSTRAINT ' + fk.name
FROM sys.foreign_keys fk
WHERE is_ms_shipped = 0
UNION
SELECT 'DROP TABLE [' + SCHEMA_NAME(schema_id) + '].[' + t.NAME + ']'
FROM sys.tables t
WHERE t.is_ms_shipped = 0
This will generate a long list of DROP .....
statements, just copy & paste those into a new SSMS window and execute them.
Wouldn't it be easier to drop/recreate the database?
DROP DATABASE yourdbname
CREATE DATABASE yourdbname
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