社区
MS-SQL Server
帖子详情
如何判断数据库中是否已经存在某个临时表?
weidegong
2004-04-30 10:10:08
如题
...全文
497
19
打赏
收藏
如何判断数据库中是否已经存在某个临时表?
如题
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
19 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
sgwindy
2004-06-15
打赏
举报
回复
经测试,下面一定正确
if exists(select * from tempdb..sysobjects where name='##临时表名称' and xtype='U')
print '存在'
else
print '不存在'
selfrich
2004-05-03
打赏
举报
回复
一、已创建的临时表只存在于tempdb库的用户表中(但不属于tempdb库),而不可能存在于某个其它库的的用户表中; 如为库A创建了一个临时表#m,但在A的表对象中并没有#m,而#m确在tempdb的表对象中(你可以在查询分析器的观察).
二、为任何库创建的临时表,表名均不能重复,因为临时表只存在于tempdb库的用户表中;
三、临时表并不存在父对象,其tempdb..sysobjects.parent_obj=0;
四、还存在临时存储过程等其它对象;所以下列判断方法不完全
IF (OBJECT_ID('TEMPDB..#') IS NOT NULL)
PRINT 'EXISTS'
ELSE
PRINT 'NOT EXISTS'
五、可以用下列方法判断是否存在相应的临时表(#m):
if exists(select 1 from tempdb..sysobjects where name='#m' and xtype='U')
print 'not exists'
else
print 'exists'
kqh0319
2004-05-03
打赏
举报
回复
学习
silversnowjing
2004-05-01
打赏
举报
回复
学习
internetcsdn
2004-05-01
打赏
举报
回复
同意hglhyy(查無此人) ( )
最简单的办法了
vileboy
2004-05-01
打赏
举报
回复
if exists(select 1 from tempdb..sysobjects where left(name,6)='#tbl_t')
print 'not exists'
else
print 'exists'
hglhyy
2004-05-01
打赏
举报
回复
如果知道临时表名,存不存在查询下不就出来了!
weidegong
2004-04-30
打赏
举报
回复
UP一下,确实不能用呀?
lalakid
2004-04-30
打赏
举报
回复
支持ghosthjt(天煞孤星)
测试了,好使
weidegong
2004-04-30
打赏
举报
回复
不行;
if exists (select * from sysobjects where id = object_id(N'[dbo].[#tmp]') and OBJECTPROPERTY(id, N'IsTable') = 1)
print 'yes'
GO
klan
2004-04-30
打赏
举报
回复
if exists (select * from sysobjects where id = object_id(N'[dbo].[你的临时表名]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
print 'yes'
GO
youngby
2004-04-30
打赏
举报
回复
create table #tmp
if @@error<>0 then
print ‘存在’
else begin print ‘不存在’ drop table #tmp end
youngby
2004-04-30
打赏
举报
回复
create table #tmp
if @@error<>0 then
print ‘存在’
else print ‘不存在’
Mybeautiful
2004-04-30
打赏
举报
回复
还有一个方法,
就是 drop table #tem
检查@@ERror_Statue 就知道了
sysmaster
2004-04-30
打赏
举报
回复
#dd 是我用来测试的临时表
sysmaster
2004-04-30
打赏
举报
回复
if object_id('tempdb..#dd') is not null
select 'yes'
else
select 'no'
已测试!
ghosthjt
2004-04-30
打赏
举报
回复
临时表只存在于tempdb中不存在于其它数据库
请测试:
use pubs
create table #tmp (aa int)
select * from sysobjects where id =object_id('#tmp')
use tempdb
select * from sysobjects where id =object_id('#tmp')
drop table #tmp
SassyBoy
2004-04-30
打赏
举报
回复
在SQL SERVER中,用select object_id('Tempdb..#TempTable')該語句可判斷一個臨時表是否已經建立。(已建立返回值為一個整數,未建立則返回Null值)
我們可在建立臨時表前先用以下語句判斷臨時表是否已存在,當已存在時就刪除該臨時表。
if not (select object_id('Tempdb..#TempTable')) is null drop table #TempTable
prcgolf
2004-04-30
打赏
举报
回复
IF (OBJECT_ID('TEMPDB..#') IS NOT NULL)
PRINT 'EXISTS'
ELSE
PRINT 'NOT EXISTS'
SQL
判断
当前
数据库
是否
存在
某个表/
临时表
–
判断
当前
数据库
是否
存在
某个表,有则删除后再新建 If Exists (select * from sysobjects where id = object_id(‘N’YourTable’) and OBJECTPROPERTY(id, ‘IsUserTable’) = 1) Drop Table YourTable Else Begin CREATE TABLE YourTable End 或者 IF OBJECT_ID(N’YourTable’,N’U’) IS NOT NULL DROP TABLE
Sql
中
判断
“
数据库
"、"表"、"
临时表
"、"存储过程"和列”
是否
存在
--
判断
数据库
是否
存在
IF EXISTS (SELECT * FROM MASTER..sysdatabases WHERE NAME = ''库名'') PRINT ''exists '' else PRINT ''not exists'' --
判断
要创建的表名
是否
存在
IF EXISTS (Select * From sysObjects Wh...
MSSQLSERVER
数据库
-
判断
全局
临时表
是否
存在
写一下今天遇到的一个问题。 今天因为一些作用域的问题,我使用了全局
临时表
,然后我在存储过程里使用了这么一段语句,想
判断
全局
临时表
是否
存在
,如果不
存在
,则将他DROP掉。 可是这段语句没用。 if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'[##temp]')) Begin ...
MS SQL
中
判断
数据库
, 存储过程,表,
临时表
,视图,函数,用户,用户创建对象 等
是否
存在
SQL脚本...
MS SQL
中
判断
数据库
, 存储过程,表,
临时表
,视图,函数,用户,用户创建对象 等
是否
存在
SQL脚本 摘自: http://www.111cn.net/database/mssqlserver/39107.htm sql
判断
存储过程
是否
存在
1...
java
中
检测
临时表
是否
存在
_如何在建
临时表
前
判断
存在
不
存在
?sql server
数据库
...
create Table #Report(ID int IDENTITY PRIMARY KEY,bookId int,bookName varchar(50))goif exists(select * from tempdb..sysobjects where id=object_id('tempdb..#Report'))select '#Report表
存在
!'elseselect '#Rep...
MS-SQL Server
34,875
社区成员
254,639
社区内容
发帖
与我相关
我的任务
MS-SQL Server
MS-SQL Server相关内容讨论专区
复制链接
扫一扫
分享
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章