How to delete tables of specific schema in sql?

Aman Sharma
0
In previous post, we have discussed how to change schema of tables(single & multiple). Sometime we have two types of schema in one database. In this article we will learn how to delete all table of specific schema.

SQL Query to delete table of schema [dbo]:


DECLARE @SqlQuery NVARCHAR(MAX)

SELECT @SqlQuery = COALESCE(@SqlQuery, N'') + N'DROP TABLE [dbo].' + QUOTENAME(TABLE_NAME) + N';' + CHAR(13)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo' and TABLE_TYPE = 'BASE TABLE'

PRINT @SqlQuery



When you execute above query, it will print list of drop table statements as given below:


DROP TABLE [dbo].[tblStudent];
DROP TABLE [dbo].[tblStaff];
DROP TABLE [dbo].[tblClass];


Now select above statements & execute to delete table of schema [dbo].


Note:  If you are using foreign key relationship then you cannot delete tables with foreign key references.

Tags

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !