存储过程中使用临时表的问题

zhuawang 2008-06-25 09:46:32
在一个存储过程中为什么这样写不行?因为我不想用太多个临时表,所以要删除了再插入数据,这样不行吗?

if(object_id('tempdb..#t') is not null)
drop table #t
select * into #t from t
if(object_id('tempdb..#t') is not null)
drop table #t
select * into #t from t

提示:数据库中已存在名为 '#t' 的对象。

已经删除了为什么还提示已存在名为 '#t' 的对象。
...全文
101 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
xupeihuagudulei 2008-06-26
  • 打赏
  • 举报
回复
nzperfect 2008-06-25
  • 打赏
  • 举报
回复
那就是你对了.
pt1314917 2008-06-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 perfectaction 的回复:]
to pt1314917
你的这个头像不好看,还不如原来的呢
[/Quote]

这个头像凶一点。嘿嘿```
Garnett_KG 2008-06-25
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 perfectaction 的回复:]
和语法有什么关系.是sql server处理机制的问题
[/Quote]

谈不上啥处理机制问题吧,语句分析都通不过了。
你在Analyzer上Ctrl+F5分析一下看能否通过??


nzperfect 2008-06-25
  • 打赏
  • 举报
回复
和语法有什么关系.是sql server处理机制的问题
Garnett_KG 2008-06-25
  • 打赏
  • 举报
回复
純粹是SQL Server语法分析器的BUG...
Garnett_KG 2008-06-25
  • 打赏
  • 举报
回复
純粹是SQL Server语法分析器的BUG...
nzperfect 2008-06-25
  • 打赏
  • 举报
回复
如果在单个存储过程或批处理中创建了多个临时表,则它们必须有不同的名称。
nzperfect 2008-06-25
  • 打赏
  • 举报
回复
drop table 临时表,后面需要加go,才能再创建这个临时表
而你的存储过程里不能用go

联机文档有文:
如果在单个存储过程或批处理中创建了多个临时表,则它们必须有不同的名称。

lff642 2008-06-25
  • 打赏
  • 举报
回复
GO
用信号通知 Microsoft® SQL Server™ 实用工具一批 Transact-SQL 语句的结束。
lff642 2008-06-25
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zhuawang 的回复:]
“存储过程里go不了。 ”是什么意思?
[/Quote]

GO是批处理的意思.
lff642 2008-06-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 perfectaction 的回复:]
存储过程里go不了。
[/Quote]
zhuawang 2008-06-25
  • 打赏
  • 举报
回复
“存储过程里go不了。 ”是什么意思?
zhuawang 2008-06-25
  • 打赏
  • 举报
回复
没有断开,我不是手动drop table #t 了吗
zhuawang 2008-06-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 pt1314917 的回复:]
SQL code
if(object_id('tempdb..#t') is not null)
drop table #t
select * into #t from a
go --注意这里
if(object_id('tempdb..#t') is not null)
drop table #t
select * into #t from a
[/Quote]

什么意思?
  • 打赏
  • 举报
回复

if(object_id('tempdb..#t') is not null)
drop table #t -------------------------->>没有执行
select * into #t from t ------------------->>所以会提示#t已经存在
if(object_id('tempdb..#t') is not null)
drop table #t
select * into #t from t

临时表只有当你的这次连接结束的时候才会删除掉。lz可以试试,你把你的这次连接断开,然后再连接,临时表就不存在了。
但是你的并没有断开连接
nzperfect 2008-06-25
  • 打赏
  • 举报
回复
to pt1314917
你的这个头像不好看,还不如原来的呢
nzperfect 2008-06-25
  • 打赏
  • 举报
回复
存储过程里go不了。
pt1314917 2008-06-25
  • 打赏
  • 举报
回复

if(object_id('tempdb..#t') is not null)
drop table #t
select * into #t from a
go --注意这里
if(object_id('tempdb..#t') is not null)
drop table #t
select * into #t from a

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧