Drop tables before creating them VS 2012 DB project

1k views Asked by At

I would like to drop the tables included in the database project before creating them so each time I publish the project I do a full build of those tables (as by default it seems to work as an incremental build).

I've seen options to drop the tables (and other entities) that exists in the target DB but not in the source project and what I would like is more or less the opposite. Drop the tables that exists in the DB project from the target before creating them.

1

There are 1 answers

5
Doug_Ivison On

A simple way to do it, is to put it in SQL code, before the CREATE TABLE:

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'zzWorkTable1')
    DROP TABLE dbo.[zzWorkTable1]

Or, if you're doing it in another database:

IF EXISTS (SELECT * FROM [<db_name>].INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'zzWorkTable1')
    DROP TABLE [<db_name>].dbo.[zzWorkTable1]

If you want some other approaches... can you tell us a little about the project, like what kind of steps create the tables?