删除表,然后无法重新创建具有相同名称的表 [英] Drop table, then cannot recreate table with the same name

查看:184
本文介绍了删除表,然后无法重新创建具有相同名称的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先在SQL Server 2008中删除一个表(此后它显示该命令已成功执行的消息)。

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.

关闭SSMS窗口并重新打开后,它尝试再次创建具有相同名称的表,并成功。

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

这是怎么回事?

推荐答案

您不能删除并在sql服务器
中的同一批中创建相同的表,请参见 MSDN

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

他们的示例使用GO分解了两个命令。半冒号可能会起作用,

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

Drop Table ...;
创建表,,;

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

Begin Transaction
Drop Table...
Commit Transaction
Create Table

当然,也可以将其拆分为两个命令,这就是GO在SQL Server管理器的查询窗口中执行的操作。

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.

这篇关于删除表,然后无法重新创建具有相同名称的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆