Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+2 votes
2 views
in SQL by (20.3k points)

The table name is Scores.

Is it correct to do the following?

IF EXISTS(SELECT *

          FROM   dbo.Scores)

  DROP TABLE dbo.Scores 

1 Answer

+4 votes
by (40.7k points)
edited by

No, the code you have mentioned isn’t correct. If you use that code, it will drop the table only if it contains any rows and it’ll result in an error if the table does not exist.

You can refer to this video for DROP Table.

You can use this code instead of a permanent table.

IF OBJECTA_ID('dbo.Scores', 'U') IS NOT NULL 

  DROP TABLE dbo.Scores; 

Or

Are you interested in learning SQL from scratch! Have a look at this interesting video on SQL provided by Intellipaat:

For a Temporary table you can use this code:

IF OBJECTA_ID('tempdb.dbo.#T1', 'U') IS NOT NULL

  DROP TABLE #T1; 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...