如何自己删除临时表

[复制链接]
查看11 | 回复4 | 2006-9-1 20:36:36 | 显示全部楼层 |阅读模式
我需要自己删除临时表,因为在一个操作流程内,我只需要将数据在3个临时表来回滚即可,比如#t1有了数据,select into #t2后,再次需要select into#t1,因此在这中间我需要检查这几个临时表是否存在,若存在就删除。
但是obejct_id(\'#t1\')或者exists(select name from tempdb.dbo.sysobjects where name=\'#t1\')都有些问题。
这需要怎么写?有啥注意事项?
回复

使用道具 举报

千问 | 2006-9-1 20:36:36 | 显示全部楼层
使用LIKE就可以,如
select name from tempdb.dbo.sysobjects where name LIKE\'#t1%\'
回复

使用道具 举报

千问 | 2006-9-1 20:36:36 | 显示全部楼层
现在搞清楚了情况,比较诡异
如果一批sql中没有用go,在一个地方用if object_id(\'tempdb.dbo.#t1\') is not null drop table #t1
的确删除了这个表,但是紧接的一个select into #t1 又说表还存在,可能是io之间的不同步造成的。这样的话,这select into #t1前加一个go就可以了。
问题是,如果这是一个存储过程的话,中间是无法加go的!!!
这个怎么办?如果每个临时表都是几个G的话,这样的空间浪费太浪费了。
回复

使用道具 举报

千问 | 2006-9-1 20:36:36 | 显示全部楼层
without go, just do

truncate table #t1
drop table #t1
then

select * into #t1
回复

使用道具 举报

千问 | 2006-9-1 20:36:36 | 显示全部楼层
最初由 taozhao 发布
[B]without go, just do

truncate table #t1
drop table #t1
then

select * into #t1 [/B]

就跟你说得这样,发现的确不行
if object_id('tempdb.dbo.#t1')
drop table #t1
select *
into #t1
from #t2
这是就说已经有#t1表了~~~~

但如果中间加go,就好了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行