Drop a table with a SQL Server script

How to drop a SQL Server table with a T-SQL script?

To drop a table with a SQL Server script, simply use the DROP TABLE command. Indeed, the syntax of the MS SQL 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 other that dbo, for database owner, is used.

Script to drop a table with SQL Server

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. »

If you DROP a table from a SQL Server database and use directly the SQL command DROP TABLE followed by the table name. If the table does not exist, then the Microsoft Database Management System (RDBMS) is throwing an error.

The solution is to test the existence of the table using a simple SQL query. Use the SQL script example to check the existence before drop a table using the EXISTS function. The previous tutorial on how to manage SQL Server tables, is on how to modify a SQL Server table using the ALTER TABLE command.

Be the first to comment

Leave a Reply

Your email address will not be published.


*