Drop a table in SQL Server

How to drop a table in SQL Server with a T-SQL command? Simply use the DROP TABLE command. Indeed, the syntax of the MSSQL command to delete a table from a database is straight forward.

Just remember to fill in the schema followed by the name of the SQL Server table in the case where a schema that dbo, for database owner, is used.

Script to drop a table in SQL Server from the database

Here is a SQL script to create a SQL Server table, use it to have a sample table in the database.

For example, let’s consider a table named dbo.SALES, and use this query to drop the table :

DROP TABLE [dbo].[SALES]; 

As no existence check is performed, if the SQL server displays this error, then the table doesn’t exist in the current database.

SQL Server Error:
« Msg 3701, Level 11, State 5, Line 1
Cannot drop the table ‘SALES’, because it does not exist or you do not have permission. »

You want to DROP a table from a SQL Server database, and use directly the SQL command DROP TABLE followed by the table name. The Microsoft Database Management System (DBMS) is throwing an error because the table does not exist. The solution is to test the existence of the table using a simple SQL query.

The previous article is on how to modify a SQL Server table.

Use the SQL script example to check the existence before drop a table using the EXISTS function.

Be the first to comment

Leave a Reply

Your email address will not be published.


*