I regularly find myself writing repetitive code in T-SQL. Some things are best done by just creating code snippets but we've also added several others to our free SDU Tools for developers and DBAs.
One straightforward one is DropTemporaryTableIfExists.
This just wraps all that's needed to remove a temporary table if it exists. The nice thing with this procedure is that you can call it before creating a temporary table, and call it again after you finish using the temporary table, as shown in the main image above.
In addition, to make it a little easier to use, you can reference the table with or without the # prefix, as shown below:
The outcome is the same in both cases.
You can also see it in action here:
To become an SDU Insider and to get our free tools and eBooks, please just visit here: