表名为Scores。

执行以下操作是否正确?

IF EXISTS(SELECT *
          FROM   dbo.Scores)
  DROP TABLE dbo.Scores

当前回答

确保在末尾使用级联约束来自动删除依赖于表的所有对象(例如视图和投影)。

drop table if exists tableName cascade;

其他回答

看过很多不太管用的。创建临时表时,必须将其从tempdb中删除!

唯一有效的代码是:

IF OBJECT_ID('tempdb..#tempdbname') IS NOT NULL     --Remove dbo here 
    DROP TABLE #tempdbname   -- Remoeve "tempdb.dbo"

确保在末尾使用级联约束来自动删除依赖于表的所有对象(例如视图和投影)。

drop table if exists tableName cascade;

我希望这有助于:

begin try drop table #tempTable end try
begin catch end catch

在SQL Server 2016(13.x)及更高版本中

DROP TABLE IF EXISTS dbo.Scores

在早期版本中

IF OBJECT_ID('dbo.Scores', 'U') IS NOT NULL 
DROP TABLE dbo.Scores; 

U是您的桌子类型

简单的是:

IF OBJECT_ID(dbo.TableName, 'U') IS NOT NULL
DROP TABLE dbo.TableName

其中dbo.TableName是所需的表,“U”是表的类型。