deltaHelper Documentation Series -- DELTA_TableExists
I will start the series with the DELTA_TableExists function that is provided in the deltaHelper script. We have implemented certain guidelines on our team for writing deltas. One guideline required that all delta be repeatable. As you may have already noticed, deltaRunner enforces this policy by executing deltas in development mode two times. Writing deltas in this way force us to think about the database prior to just making changes. We use this function in our delta files to ensure that we are only adding a table to the database once. To make changes to a table, we will use a procedure called DELTA_ColumnAdder or one of a few other variants. More on this in a later post.
The DELTA_TableExists function provides an easy way to determine if a table already exists in the database. Lets say for any number of reasons you wanted to know if a table exists in your database. In this example we will do some work if we know a specified table already exists in the database.
if(dbo.DELTA_TableExists('TABLENAME') = 1)
begin
-- TABLE EXISTS ... DO SOME WORK
end
go
Or you might want to check to see the table does not exists.
if(dbo.DELTA_TableExists('TABLENAME') = 0)
begin
-- TABLE DOES NOT EXIST ... DO SOME WORK
end
go