In a script used to interactively analyze subsets of data, it is often useful to store query results in temporary tables for further analysis.
Many of my analysis scripts contain this structure:
CREATE TABLE
In SQL Server, temporary tables are tied to joins, so the query results are saved after the initial query. When a subset of the data that I want to analyze is expensive to calculate, I use this method instead of using a table variable, because the subset is stored in different batches of queries.
Part of the setup script is run once, and the following queries ( SELECT * FROM #Results are the SELECT * FROM #Results here) are executed as often as possible.
Sometimes I want to update a subset of the data in a temporary table, so I run the whole script again. One way to do this is to create a new connection by copying the script into a new request window in Management Studio, I find it difficult to handle.
Instead, the usual workaround is to precede the create statement with the conditional drop statement as follows:
IF OBJECT_ID(N'tempdb.dbo.#Results', 'U') IS NOT NULL BEGIN DROP TABLE
This statement correctly handles two situations:
- On the first run, when the table does not exist: do nothing.
- In subsequent launches, when the table exists: drop the table.
Production scripts written by me will always use this method because it does not cause errors in two expected situations.
Some equivalent scripts written by my fellow developers sometimes handle these two situations using exception handling:
BEGIN TRY DROP TABLE
I believe in the world of databases that itβs always better to ask permission than to seek forgiveness, so this method makes me embarrassed.
The second method swallows the error, without taking any action to handle non-exceptional behavior (the table does not exist). In addition, it is possible that the error will be raised for another reason, except that the table does not exist.
Wise Owl warns about this:
Of the two methods, the [ OBJECT_ID ] method is harder to understand, but probably better: using the [ BEGIN TRY ] method, you run the risk of catching the wrong error!
But this does not explain the practical risks.
In practice, the BEGIN TRY method has never caused a problem on the systems that I support, so I'm glad it stayed there.
What are the dangers for managing temporary table existence using the BEGIN TRY method? What unexpected errors can be hidden by an empty catch block?