DEV Community

CodetoLive blog
CodetoLive blog

Posted on • Originally published at codetolive.in on

How to check if a temporary table exists and delete it if it does before creating a temporary table?

You can check if the previously created temporary table exists before creating the same table again in your stored procedure. Drop them if they exist.

Code:

// checks that the temporary table 'results' exists in the SQL tempdb.
IF OBJECT_ID('tempdb..#Results') IS NOT NULL DROP TABLE #Results
GO

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
brunotdantas_br profile image
Bruno Dantas • Edited

Another way is

DROP TABLE IF EXISTS #YOUR_TEMP_TABLE

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay