为什么临时表会无效(50分)

  • 主题发起人 主题发起人 小田
  • 开始时间 开始时间

小田

Unregistered / Unconfirmed
GUEST, unregistred user!
declare @sqlstring nvarchar(3000)
if exists(select name from tempdb..sysobjects where name like '#aaa%')
drop table #aaa
set @sqlstring='select clid into #aaa from dbo.zl group by clid'
exec (@sqlstring)
select * from #aaa
请问:为什么#aaa临时表会无效的呢?
 
declare @sqlstring nvarchar(3000)
if exists(select name from tempdb..sysobjects where name like '#aaa%')
drop table #aaa
//set @sqlstring='select clid into #aaa from dbo.zl group by clid'
//exec (@sqlstring)
//上面两句换为直接运行!
select clid into #aaa from dbo.zl group by clid
select * from #aaa
注:本地临时表仅在当前会话中可见,并且将在退出其作用域时由系统自动除去。
 
问题是一定要用exec啊,不然我问什么,呵呵
 
declare @sqlstring nvarchar(3000)
if exists(select name from tempdb..sysobjects where name like '#aaa%')
drop table #aaa
set @sqlstring='select clid into #aaa from dbo.zl group by clid' + char(13)+
'go '+char(13)+
'select * from #aaa'
exec (@sqlstring)
 
那我怎么怎样使用#aaa呢:
.....
select * from #aaa...
......
我用#aaa表还要做个查询
 
版主,回答无效,请还分,thanks!
 
可以写为
exec('select clid into #aaa from dbo.zl group by clid select * from #aaa')
 
exec (@sqlstring+' select * from #aaa ')
exec中的临时表在exec结束后消失
 
多人接受答案了。
 
后退
顶部