Select 1 as a into ##tmp if exists(select * from tempdb..sysobjects where name = '##tmp') print 'a' else print 'b'drop table ##tmp
if object_id('tempdb..##temp') is not null drop table #temp
if object_id('tempdb..#temptablename') is not null print 'exist'
Select 1 as a into ##tmp if exists(select * from tempdb..sysobjects where name = '##tmp') print 'a' else print 'b'drop table ##tmp
use pubs select * into #tempa from jobs goif object_id('tempdb..#tempa') is not null print 'exist'
if exists(select 1 from tempdb..sysobjects where name='##temp1' and xtype='U') print '临时表已经存在' else print '不存在'
use pubs select * into #tempa from jobs go select * into ##tempa from jobs goif object_id('tempdb..#tempa') is not null print 'exist' if object_id('tempdb..##tempa') is not null print 'ok'
请注意用sysobjects只可以判断全局临时表
if exists(select top 1 * from #temp) print '有数据'
--呵呵,综合大家的方法if object_id('##temp') is not null begin if exists(select top 1 * from ##temp) print '有数据' else print '无数据' end else print '表不存在'
if exists(select * from tempdb..sysobjects where name = '##tmp')
print 'a'
else
print
'b'drop table ##tmp
drop table #temp
print 'exist'
if exists(select * from tempdb..sysobjects where name = '##tmp')
print 'a'
else
print
'b'drop table ##tmp
use pubs
select * into #tempa from jobs
goif object_id('tempdb..#tempa') is not null
print 'exist'
print '临时表已经存在'
else
print '不存在'
select * into #tempa from jobs
go
select * into ##tempa from jobs
goif object_id('tempdb..#tempa') is not null
print 'exist'
if object_id('tempdb..##tempa') is not null
print 'ok'
print '有数据'
begin
if exists(select top 1 * from ##temp)
print '有数据'
else
print '无数据'
end
else
print '表不存在'