Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Assuming I have a database column containing table names, how do I safely delete all of the tables it lists?

The tables themselves are fairly simple - they may have indeces, but definitely don't contain other complex items (and don't have dependencies on other tables - ie, foreign keys into/out of them, etc...)

share|improve this question
up vote 4 down vote accepted

Something along the lines of the following should work.

CREATE TABLE dbo.TablesToDrop
    TableName SYSNAME
INSERT INTO dbo.TablesToDrop VALUES ('Table1');
INSERT INTO dbo.TablesToDrop VALUES ('Table2');
INSERT INTO dbo.TablesToDrop VALUES ('Table3');
SET @Cmd = '';
SELECT @Cmd = @Cmd + 'DROP TABLE ' + QUOTENAME(t.TableName) + ';'
FROM dbo.TablesToDrop T;
--EXEC sp_executesql @Cmd;

Uncomment the last line to actually execute the DROP TABLE statements.

share|improve this answer
Please read this answer by Martin Smith, then stop using variable concatenation, even for 'simple' uses. It's a bad habit you should kick :) – Paul White May 17 '14 at 7:39

if your database column contains below table names:


Then you can use:

select 'drop table '+quotename(column_name) + char(10) +'go'from table_name

In text mode it will generate sql to drop them. Execute the generated sql by VERIFYING that you indeed want to drop the tables in another query window.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.