Skip to main content

Examples of of how to create of delete a temp table in SQL Server, depending on the temp table's existence.

--
-- To create a temp table (#Users) if it doesn't already exist:
if OBJECT_ID('tempdb..#Users') is null
    begin
        create table #Users(
            id int not null,
            username varchar(50) not null
        );
    end;
go

--
-- To delete a temp table (#Users) if it exists:
if OBJECT_ID('tempdb..#Users') is not null
    begin
        drop table #Users;
    end;
go