注册 登录
编程论坛 SQL Server论坛

[求助]如何判断临时表是否存在?

cyc308 发布于 2007-02-07 14:24, 2192 次点击

如何判断SQL临时表是否存在?
如何判断表是否存在?

5 回复
#2
棉花糖ONE2007-02-07 14:37
if object_id('tempdb..#a') is not null
print 'exists'
else
print 'not exists'
临时表存在tempdb数据库中,你照着这个判断吧
#3
cyc3082007-02-07 16:48

为什么这个存储过程无法判断#a8临时表是否存在呢?
CREATE PROCEDURE test_temptable AS
if object_id('tempdb..#a8') is null
begin
select * into #a8 from c_admin
end
GO

#4
Kendy1234562007-02-07 17:12

因为#a8不是在这个存储过程中创建的!

临时表只在当前的connection内能查到

你可以试试 在查询分析器里面开2个连接窗口,
windows1:

select col1= 1 into #1

select * from #1

if object_id('tempdb..#1') is not null
print 'exists'
else
print 'not exists'

会得到 exist2

这时候不drop #1, 去windows2 运行:
if object_id('tempdb..#1') is not null
print 'exists'
else
print 'not exists'

会看到 not exist


也就是说对于临时表 只有创建它的connection才能看见它

#5
棉花糖ONE2007-02-07 18:07

create table ##a(id int)

if object_id('tempdb..##a') is not null
print 'exists'
else
print 'not exists'

使用全局临时表,当连接断开后,全局临时表也会消失

[此贴子已经被作者于2007-2-7 18:17:30编辑过]

#6
smilerapple2007-02-07 18:18
真是厉害啊
1