devxlogo

Dropping Tables at Beginning of Statement

Dropping Tables at Beginning of Statement

Question:
I am trying to create a procedure that first drops the table “table” but only if it already exists.

How do I create the statement using “if exists” or something similar to this?

 Drop table mytableselect * into mytablefrom othertable

This statement returns an error if the table does not already exist.

Answer:
What you are after is this:

 IF EXISTS (SELECT name FROM sysobjects WHERE type = 'U')    DROP TABLE mytableEND

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist