sql判断临时表是否存在

2025-03-23 18:37:49
推荐回答(3个)
回答(1):

使用tempdb中的表sysobjects 来查询,判断。

1、新建临时表#test

CREATE TABLE #test(
id char(10) NOT NULL, 
a int NOT NULL, 
b datetime NOT NULL, 
c char(10) NULL)

 2、判断语句如下

select case when count(*) = 1 then '表存在' else '表不存在' end 
from tempdb..sysobjects 
where id=object_id('tempdb..#test')

 

回答(2):

--下面以临时表#temp为例,判断它是否存在,存在就删除它
IF OBJECT_ID('tempdb..#temp') is not null
drop table #temp

回答(3):

。。。。临时表以#开始 create table #a(ID int,Name varchar(50))
临时表是放在临时数据库的 当sqlserver断开连接后 将自动删除临时表 此时你是找不到的