sql server if temp table exists code example

Example 1: sql server drop temp table if exists

IF OBJECT_ID('tempdb..#Results') IS NOT NULL DROP TABLE #Results
GO

Example 2: sql server check if temp table exists

IF OBJECT_ID('tempdb..#Results') IS NOT NULL DROP TABLE #Results

Example 3: sql server check table exists

IF EXISTS 
  (SELECT object_id FROM sys.tables
  WHERE name = 'Artists'
  AND SCHEMA_NAME(schema_id) = 'dbo')
  PRINT 'The table exists'
ELSE 
  PRINT 'The table does not exist';

Example 4: check if sql temp table exists

IF OBJECT_ID('tempdb..#Results') IS NOT NULL
    Truncate TABLE #Results
else
    CREATE TABLE #Results
    (
        Company             CHAR(3),
        StepId              TINYINT,
        FieldId             TINYINT,
    )

Tags:

Sql Example