用临时表时碰到错误
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里。请问如何在调试时清除临时表。谢谢。

解决方案 »

  1.   


    if OBJECT_ID('#TblProdOrders','U') is not null drop table #TblProdOrders
      

  2.   

    抢分~~呵呵
    if OBJECT_ID('tempdb..#TblProdOrders') is not null
    drop table #TblProdOrders
      

  3.   


    if OBJECT_ID('tempdb..#TblProdOrders') is not null drop table #TblProdOrders
      

  4.   

    还有运行在tempdb数据库里面的说法???
      

  5.   

    是的,可以用批处理切换到tempdb库中删掉临时表,然后再切回来
      

  6.   

    if OBJECT_ID('#TblProdOrders') is not null
       drop table #TblProdOrders
    go
       create table #TblProdOrders 
       .......没必要加那么多东西,不过加上还是比较保险一点。
      

  7.   

    临时表本来就在tempdb里面运行。回去看书去
      

  8.   

    专业一点应该叫“会话(session)”而不是线程
      

  9.   

    很好用。这里有个进一步问题:
    为何我执行
    if OBJECT_ID('tempdb..#TblProdOrders') is not null
    drop table tempdb..#TblProdOrders
    后,报
    Database name 'tempdb' ignored, referencing object in tempdb.
    只是临时表确实被清除了。drop table时甚至都不需要引用tempdb,为什么?
      

  10.   

    很好用。这里有个进一步问题:
    为何我执行
    if OBJECT_ID('tempdb..#TblProdOrders') is not null
    drop table tempdb..#TblProdOrders
    后,报
    Database name 'tempdb' ignored, referencing object in tempdb.
    只是临时表确实被清除了。drop table时甚至都不需要引用tempdb,为什么?
      

  11.   

    报错?是因为drop table tempdb..#加了tempdb吧
    因为临时表虽然是存储于tempdb,但是在当前会话中是全局引用,所有库都引用到
    #TblProdOrders应该只是个映射的别名,实际存储的并不是#TblProdOrders