Sample Header Ad - 728x90

What is the correct way to check and delete temp table?

-1 votes
1 answer
19874 views
Approach 1: IF OBJECT_ID('tempdb..#MyTempTbl') IS NOT NULL DROP TABLE #MyTempTbl; Approach 2: IF EXISTS (SELECT * FROM [tempdb].[sys].[objects] WHERE [name] = N'#MyTempTbl') DROP TABLE [#MyTempTbl]; What is the correct way to check and delete temp table? The context is a stored procedure invoked by an agent job. I have tried querying the [tempdb].[sys].[objects] and notice that the global temp table gets the same name, where as a local temp table gets name with underscores at the end like this MyTempTbl______. So I was wondering whether there is a standard way to check if temp table exists and if so to drop it, I am looking for syntax that would work for both local and global temp tables.
Asked by variable (3590 rep)
Jan 31, 2023, 03:27 PM
Last activity: Mar 15, 2024, 09:25 AM