SQL Server snippets

How to drop a temporary table if it exists

Example 1

Let's assume we have a #Schools table and we want to DROP #Universities table (that doesn't exist) without having an error as output.

CREATE TABLE #Schools(
[Name] varchar(250),
[Grade] varchar(150),
[City] varchar(150));
IF OBJECT_ID('tempdb..#Universities') IS NOT NULL 
DROP TABLE #Universities

You can find an interactive version of this example following this link .

Back to SQL Server cookbook page