SQL Server 臨時表的刪除

字號:


    1、錯誤的刪除操作:
    --錯誤的臨時表刪除操作,因為所在數(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
    --錯誤的臨時表刪除操作,因為臨時表名已變
    if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'[#temptable]'))
    Begin
    drop table #temptable
    End
    2、正確的刪除方式:
    --正確的臨時表刪除操作
    if object_id('tempdb#tempTable') is not null Begin
    drop table #tempTable
    End