Monday, June 6, 2011

SQL Server: 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  local temporary tables and global temporary tables.
The name of local temp tables prefix with '#' and global temp tables prefix with that of '##'.

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

IF OBJECT_ID('tempdb.dbo.##globalTempTable') IS NOT NULL THEN
BEGIN   
           DROP TABLE ##globalTempTable
END IF
CREATE TABLE  ##globalTempTable(ID INT NOT NULL)
GO

Reference: DP (http://dptechnicalblog.blogspot.com/ )

No comments:

Post a Comment