sql server check if temp table exists

In this SQL Tutorial I will show you how to using SQL Server check if temp table exists and drop it if it does.

SQL Server allows you to create #temp tables (# = local temp table) that exist for your connection only. There are advantages of using temp tables over table variables and vice versa but I won't be going into the details here.

I will use SQL Server 2008 R2 for this tutorial but it should work with SQL Server 2005 and 2012.

Below is full example that checks if temp table objects exists and if it does it drops it.

IF object_id('tempdb..#TableName') IS NOT NULL

BEGIN

  DROP TABLE #TableName

END

 

CREATE TABLE #TableName (FieldName VARCHAR(100))

 That is all you need to do to check if temp table exists, drop it and create it again.

I hope this SQL tutorial will help you with your T-SQL development.

 

Take care

Emil


SHARE:


No data was returned.


Disclaimer: While every caution has been taken to provide our readers with most accurate information and honest analysis, please use your discretion before taking any decisions based on the information in this blog. Author will not compensate you in any way whatsoever if you ever happen to suffer a loss/inconvenience/damage because of/while making use of information in this blog.