表名为Scores。

执行以下操作是否正确?

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

当前回答

从SQL Server 2016,您可以使用

DROP TABLE IF EXISTS dbo.Scores

参考:DROP IF EXISTS-SQL Server 2016中的新事物

它将很快出现在SQL Azure数据库中。

其他回答

简单的是:

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

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

Or:

if exists (select * from sys.objects where name = 'Scores' and type = 'u')
    drop table Scores

我希望这有助于:

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

SQL Server 2016及以上版本最好、最简单的版本是删除表(如果存在)[表名称]

Ex:

DROP TABLE IF EXISTS dbo.Scores

如果假设上面的方法不起作用,那么可以使用下面的方法

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

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

唯一有效的代码是:

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