而不是像这样检查临时表的存在:
IF OBJECT_ID('tempdb..#table') IS NOT NULL
BEGIN;
DROP TABLE #table;
END;
我正在使用新的DROP IF EXISTS
技术:
DROP TABLE IF EXISTS tempdb..#table;
它工作得很好,但是如果表不存在,我将得到以下消息。
忽略数据库名“tempdb”,它引用了tempdb中的对象。
有人知道这条信息是什么意思吗?
发布于 2018-01-12 09:23:01
在谈论tempdb
时不需要指定#table
--临时表已经在tempdb
中了。我同意这条信息是令人困惑的,但它实际上并不是一个错误--它只是一条消息(PRINT
),告诉您您做错了什么。实际上,无论消息是否存在,都会得到该消息;例如:
-- drop when doesn't exist
drop table if exists tempdb..#foo
go
-- drop when does exist
create table #foo (id int)
drop table if exists tempdb..#foo
输出消息两次
Database name 'tempdb' ignored, referencing object in tempdb.
Database name 'tempdb' ignored, referencing object in tempdb.
所以:只需使用:
DROP TABLE IF EXISTS #table;
这就是它想让你做的。
https://stackoverflow.com/questions/48222976
复制相似问题