# Drop tables from selected schemas database wide

In my previous post called “How to drop tables database wide using T-SQL” I’ve explained that how we could delete all tables database wide in SQL Server. But, what if there are some other schemas other than “dbo” and some tables that are created in those schemas? If we just delete “[dbo].” from the code we will obviously face to the following error message: “Cannot find the object “TABLE_NAME” because it does not exist or you do not have permissions.” for those tables that are not under “dbo” schema. So, we need to retrieve all schemas and their related tables. This approach is really helpful when we are implementing an ETL process that is designed to delete the tables from particular schemas and recreate the tables and populate new data. This is a common way for staging to delete and recreate tables instead of updating existing tables as update is really costly.

So, let’s write some codes.

Note: The following code is written to delete the tables under a specific schema. If you want to use it to delete all tables from all schemas you need to first remark/delete the “where” clauses from the code.

declare @FK table (No int, tbl nvarchar(max), fk nvarchar(max), scm nvarchar(max))

declare @counter int

declare @sql nvarchar(max)

declare @table nvarchar(max)

declare @Constraint nvarchar(max)

declare @schemas nvarchar(max)

insert into @FK (No, tbl , fk, scm)

select ROW_NUMBER() OVER (ORDER BY f.name),t.name, f.name, s.name from sys.foreign_keys f inner join sys.tables t on f.parent_object_id=t.object_id inner join sys.schemas s on s.schema_id=t.schema_id where s.name=‘SCHEMA_NAME’

set @counter= (select count(*) from @FK)

while @counter >0

begin

set @table = (select tbl from @FK where No=@counter)

set @Constraint = (select fk from @FK where No=@counter)

set @schemas = (select scm from @FK where No=@counter)

set @sql = ‘ALTER TABLE [‘+@schemas+‘].[‘+@table+‘]  DROP  CONSTRAINT [‘+@Constraint+‘]’

exec SP_EXECUTESQL @sql

set @counter=@counter1

end

delete from @FK

insert into @FK (No, tbl, fk, scm)

select ROW_NUMBER() OVER (ORDER BY t.name),t.name, fk=null, s.name from sys.tables t inner join sys.schemas s on s.schema_id=t.schema_id  where s.name=‘SCHEMA_NAME’

set @counter= (select count(*) from @FK)

while @counter >0

begin

set @table = (select tbl from @FK where No=@counter)

set @sql = ‘DROP TABLE [‘+@schemas+‘].[‘+@table+‘]’

exec SP_EXECUTESQL @sql

set @counter=@counter1

end;
{"ft_lang_label":"__label__en","ft_lang_prob":0.7642327,"math_prob":0.7666979,"size":2407,"snap":"2022-05-2022-21","text_gpt3_token_len":619,"char_repetition_ratio":0.15189347,"word_repetition_ratio":0.17127071,"special_character_ratio":0.2517657,"punctuation_ratio":0.116883114,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9705976,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-16T12:50:44Z\",\"WARC-Record-ID\":\"<urn:uuid:7f6ac7e8-efc8-4cf5-a0f4-981d13d9f32a>\",\"Content-Length\":\"124648\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:61e5679f-249f-4610-8b0b-1640a70ad089>\",\"WARC-Concurrent-To\":\"<urn:uuid:092a4e14-4da2-4dda-a53f-e6acffb76f55>\",\"WARC-IP-Address\":\"20.213.18.63\",\"WARC-Target-URI\":\"http://biinsight.com/drop-tables-from-selected-schemas-database-wide/\",\"WARC-Payload-Digest\":\"sha1:HG4GJH6SGQSQORONIV7DDD2LMT24IYWH\",\"WARC-Block-Digest\":\"sha1:CXCYAWQSL64IEGYGB2XX4Y6VRMLEYRHT\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662510117.12_warc_CC-MAIN-20220516104933-20220516134933-00442.warc.gz\"}"}