Labels

Wednesday, April 3, 2013

Temp Tables Existance Check



--One of the breaking changes added in SQL Server 2012 is related to the creation of #TEMP tables.
--SQL Server 2012 assigns a negative value as the OBJECT ID of the #TEMP tables.

CREATE TABLE #cust (Col1 INT)
GO

-- Right Way:

IF OBJECT_ID('tempdb..#cust', 'U') IS NOT NULL
BEGIN
    PRINT 'Object Exists'
    -- Do Something
END


-- Wrong Way if used in SQL 2012

IF OBJECT_ID('tempdb..#cust') > 0
BEGIN
    PRINT 'Object Exists'
    -- Do Something
END

No comments:

Post a Comment