sql server 臨時(shí)表的刪除

字號:


    1、錯(cuò)誤的刪除操作:
    --錯(cuò)誤的臨時(shí)表刪除操作,因?yàn)樗跀?shù)據(jù)庫不同
    if exists (select * from sysobjects where object_id = object_id(n'[dbo].[#temptable]') and type in (n'u'))
    begin
    drop table [dbo].[temptable]
    end
    --錯(cuò)誤的臨時(shí)表刪除操作,因?yàn)榕R時(shí)表名已變
    if exists (select * from tempdb.dbo.sysobjects where id = object_id(n'[#temptable]'))
    begin
    drop table #temptable
    end
    2、正確的刪除方式:
    --正確的臨時(shí)表刪除操作
    if object_id('tempdb#temptable') is not null begin
    drop table #temptable
    end