用临时表时碰到错误
Msg 2714, Level 16, State 6, Line 3
There is already an object named '#TblProdOrders' in the database.
然后我在代码前加了一段。
if OBJECT_ID('#TblProdOrders') is not null
drop table #TblProdOrders
但明显这段不起作用,我想应该是因为临时表不是运行于当下的数据库,而是在tempdb里。请问如何在调试时清除临时表。谢谢。
Msg 2714, Level 16, State 6, Line 3
There is already an object named '#TblProdOrders' in the database.
然后我在代码前加了一段。
if OBJECT_ID('#TblProdOrders') is not null
drop table #TblProdOrders
但明显这段不起作用,我想应该是因为临时表不是运行于当下的数据库,而是在tempdb里。请问如何在调试时清除临时表。谢谢。
if OBJECT_ID('#TblProdOrders','U') is not null drop table #TblProdOrders
if OBJECT_ID('tempdb..#TblProdOrders') is not null
drop table #TblProdOrders
if OBJECT_ID('tempdb..#TblProdOrders') is not null drop table #TblProdOrders
drop table #TblProdOrders
go
create table #TblProdOrders
.......没必要加那么多东西,不过加上还是比较保险一点。
为何我执行
if OBJECT_ID('tempdb..#TblProdOrders') is not null
drop table tempdb..#TblProdOrders
后,报
Database name 'tempdb' ignored, referencing object in tempdb.
只是临时表确实被清除了。drop table时甚至都不需要引用tempdb,为什么?
为何我执行
if OBJECT_ID('tempdb..#TblProdOrders') is not null
drop table tempdb..#TblProdOrders
后,报
Database name 'tempdb' ignored, referencing object in tempdb.
只是临时表确实被清除了。drop table时甚至都不需要引用tempdb,为什么?
因为临时表虽然是存储于tempdb,但是在当前会话中是全局引用,所有库都引用到
#TblProdOrders应该只是个映射的别名,实际存储的并不是#TblProdOrders