Drop table, then cannot recreate table with the same name

拜拜、爱过 提交于 2020-01-05 02:29:16

问题


I first drop a table in SQL Server 2008 (after that it shows the message that the command was executed sucessfully).

I then tried to create a table with the same name, and it showed me an error.

After closing the SSMS window and re opening it it tried to create the table with the same name again and it succeeded.

What is going on?


回答1:


You can't drop and create the same table in the same batch in sql server see MSDN

Their examples use GO to break up the two commands. Semi colon might work,

Drop Table ...; Create Table ,,,;

as might

Begin Transaction
Drop Table...
Commit Transaction
Create Table

Or of course splitting it up into two commands, which is what GO does in SQL server manager's query window.

If you do split it up, it might be wise to check whether the table exists before trying to drop it, and that it doesn't before trying to create it.



来源:https://stackoverflow.com/questions/10550488/drop-table-then-cannot-recreate-table-with-the-same-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!