表名为Scores。
执行以下操作是否正确?
IF EXISTS(SELECT *
FROM dbo.Scores)
DROP TABLE dbo.Scores
表名为Scores。
执行以下操作是否正确?
IF EXISTS(SELECT *
FROM dbo.Scores)
DROP TABLE dbo.Scores
当前回答
Or:
if exists (select * from sys.objects where name = 'Scores' and type = 'u')
drop table Scores
其他回答
在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 exists (select *
from sys.tables
where name = 'tableName'
and schema_id = schema_id('dbo'))
begin
drop table dbo.tableName
end
确保在末尾使用级联约束来自动删除依赖于表的所有对象(例如视图和投影)。
drop table if exists tableName cascade;
执行以下操作是否正确?如果存在(选择*来自数据库分数)下降表数据分数
否。只有当表包含任何行时,才会删除该表(如果表不存在,则会引发错误)。
相反,对于永久表,可以使用
IF OBJECT_ID('dbo.Scores', 'U') IS NOT NULL
DROP TABLE dbo.Scores;
或者,对于临时表,您可以使用
IF OBJECT_ID('tempdb.dbo.#TempTableName', 'U') IS NOT NULL
DROP TABLE #TempTableName;
SQL Server 2016+有更好的方法,使用DROP TABLE IF EXISTS…。请看@Jovan的回答。
看过很多不太管用的。创建临时表时,必须将其从tempdb中删除!
唯一有效的代码是:
IF OBJECT_ID('tempdb..#tempdbname') IS NOT NULL --Remove dbo here
DROP TABLE #tempdbname -- Remoeve "tempdb.dbo"