Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I resolve the "Table 'dbo.Foo' already exists." error when the table does not exist?

I've created a table and then realised I made a mistake. SSMS wouldn't let me update the table without recreating it, so I've deleted the table and then tried to create it again.

It won't let me do this, and I get an error dialog

Table 'dbo.Foo' already exists.

So I try to delete it again:

drop table dbo.Foo 

Cannot drop the table 'dbo.Foo', because it does not exist or you do not have permission.

Refreshing the IntelliSense cache does not help.

like image 815
user247702 Avatar asked Oct 31 '13 09:10

user247702


People also ask

What happens if you drop a table that doesn't exist?

The DROP TABLE statement deletes the specified table, and any data associated with it, from the database. The IF EXISTS clause allows the statement to succeed even if the specified tables does not exist. If the table does not exist and you do not include the IF EXISTS clause, the statement will return an error.

What happens if you create a table that already exists?

Answer. When you try to create a table with an already existing table name, you will receive an error message, and no table will be modified or created.

How do you check if a table exists or not?

To check if a table exists in SQL Server, you can use the INFORMATION_SCHEMA. TABLES table. You can use this table with an IF THEN clause do determine how your query responds whether or not a table exists. One of the more common uses I find for this when I need to create a table in a script.


1 Answers

Closing and restarting SSMS seems to be the only way to get rid of the error. After doing so, I can again successfully create the table.

like image 70
user247702 Avatar answered Oct 21 '22 15:10

user247702