Search

Saturday, August 20, 2011

Checking Existence and to Drop Temp Tables Checking Existence and to Drop Temp Tables

Temporary tables are useful for storing the data temporarily during the data processing. There are two types of temp tables available in SQL Server namely global temporary tables and local temporary tables. 
The name of Global temp tables prefix with '##' and Local temp tables prefix with that of '#'.

To check existence of a Global temparary table, drop and recreate the same use below approach.
IF OBJECT_ID('tempdb.dbo.##globalTempTable') IS NOT NULL THEN
BEGIN 
           DROP TABLE ##globalTempTable
END IF
CREATE TABLE  ##globalTempTable(ID INT NOT NULL)
GO

To check existence of a Local temparary table, drop and recreate the same use below approach.
IF OBJECT_ID('tempdb.dbo.#localTempTable') IS NOT NULL THEN
BEGIN 
           DROP TABLE #localTempTable
END IF
CREATE TABLE  #localTempTable(ID INT NOT NULL)
GO

No comments:

Post a Comment