临时table既存在又不存在
Temporary table both exists and doesn't exist
我正在尝试在 SQL 服务器中删除一个临时 table(如果它存在)并创建一个新的临时 table(如果它不存在)。命令 IF OBJECT_ID('tempdb.. #my_temp') IS NULL
似乎无法确定临时 table,然后当查询尝试创建 table 时,我收到一个错误,指出临时 table 已经存在。
这是我的查询:
IF OBJECT_ID('tempdb.. #my_temp') IS NOT NULL
BEGIN
DROP TABLE #my_temp
END
IF OBJECT_ID('tempdb.. #my_temp') IS NULL
BEGIN
print 'trying to create a new temp table'
CREATE TABLE #my_temp
( [ID] int NOT NULL PRIMARY KEY CLUSTERED ,
) ON [PRIMARY]
END
如果我 运行 这个查询两次,我第二次收到这个输出:
trying to create a new temp table
Msg 2714, Level 16, State 6, Line 12
There is already an object named '#my_temp' in the database.
我的 SQL 服务器是 2019 版供您参考。
删除 .. 和 table 名称之间的 space
IF OBJECT_ID('tempdb..#my_temp') IS NOT NULL
BEGIN
DROP TABLE #my_temp
END
IF OBJECT_ID('tempdb..#my_temp') IS NULL
BEGIN
print 'trying to create a new temp table'
CREATE TABLE #my_temp
( [ID] int NOT NULL PRIMARY KEY CLUSTERED ,
) ON [PRIMARY]
END
我正在尝试在 SQL 服务器中删除一个临时 table(如果它存在)并创建一个新的临时 table(如果它不存在)。命令 IF OBJECT_ID('tempdb.. #my_temp') IS NULL
似乎无法确定临时 table,然后当查询尝试创建 table 时,我收到一个错误,指出临时 table 已经存在。
这是我的查询:
IF OBJECT_ID('tempdb.. #my_temp') IS NOT NULL
BEGIN
DROP TABLE #my_temp
END
IF OBJECT_ID('tempdb.. #my_temp') IS NULL
BEGIN
print 'trying to create a new temp table'
CREATE TABLE #my_temp
( [ID] int NOT NULL PRIMARY KEY CLUSTERED ,
) ON [PRIMARY]
END
如果我 运行 这个查询两次,我第二次收到这个输出:
trying to create a new temp table Msg 2714, Level 16, State 6, Line 12 There is already an object named '#my_temp' in the database.
我的 SQL 服务器是 2019 版供您参考。
删除 .. 和 table 名称之间的 space
IF OBJECT_ID('tempdb..#my_temp') IS NOT NULL
BEGIN
DROP TABLE #my_temp
END
IF OBJECT_ID('tempdb..#my_temp') IS NULL
BEGIN
print 'trying to create a new temp table'
CREATE TABLE #my_temp
( [ID] int NOT NULL PRIMARY KEY CLUSTERED ,
) ON [PRIMARY]
END