请指点一个存储过程的问题??

WangZWang 2004-07-14 11:07:00
在执行下面的语句时为何出现提示'对象#YBReport无效',对象#YBReport
我已经建立了, 还有没有动态建立表的方法, 谢谢!

Create procedure up_xxx
@IfDetail int
As
Set NoCount on
Declare @CreateStr nvarchar(4000)

If @IfDetail=0
Begin

Set @CreateStr=' Create Table #YBReport(
DeptCode varchar(20),
Finance Char(2),
SumMoney Money,
tzrc Int,
tzFare Money,
tzAvgFare Money
)
CREATE INDEX [IX_#YBReport] ON [dbo].[#YBReport]([DeptCode]) ON [PRIMARY]'
End
Else
Begin
Set @CreateStr='Create Table #YBReport(
DeptCode varchar(20),
Sickid Int,
Visitid Int,
SickCode varchar(10),
SickName varchar(16),
Finance Char(2),
SumMoney Money,
tzFare Money,
tzAvgFare Money
)
CREATE INDEX [IX_#YBReport] ON [dbo].[#YBReport]([DeptCode],[Sickid],[VisitID]) ON [PRIMARY]'
End

Exec(@CreateStr)

Select * from #YBReport

truncate table #YBReport
drop table #YBReport

go

Exec up_xxx 0
...全文
96 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zheninchangjiang 2004-07-14
  • 打赏
  • 举报
回复
那也没有办法,在EXEC外就是无效
WangZWang 2004-07-14
  • 打赏
  • 举报
回复
在中间有一段语句,我要先对临时表#ybreport进行操作??
47522341 2004-07-14
  • 打赏
  • 举报
回复
#ybreport在執行語句Exec(@CreateStr)後就被釋放掉了;
局部臨時變量不能這麼用的;
解決方式就是按照頂樓的兄弟的意思;
將Select * from #YBReport

truncate table #YBReport
drop table #YBReport
也加入動態執行語句中就可以了;
wtadminxjeri 2004-07-14
  • 打赏
  • 举报
回复
Exec(@CreateStr) --执行完这步,实际上#YBReport已经消失了

Select * from #YBReport
liang922 2004-07-14
  • 打赏
  • 举报
回复
应该这样
set @CreateStr=@CreateStr +
' select * from #ybreport
truncate table #YBReport
drop table #YBReport
'

exec (@CreateStr)
zheninchangjiang 2004-07-14
  • 打赏
  • 举报
回复
将最后的那三句也写进 @createstr,#t 只在exec()过程中有效
WangZWang 2004-07-14
  • 打赏
  • 举报
回复
在执行下面的语句时为何出现提示'对象#YBReport无效',对象#YBReport
我已经建立了, 还有没有动态建立表的方法, 谢谢!

Create procedure up_xxx
@IfDetail int
As
Set NoCount on
Declare @CreateStr nvarchar(4000)

If @IfDetail=0
Begin

Set @CreateStr=' Create Table #YBReport(
DeptCode varchar(20),
Finance Char(2),
SumMoney Money,
tzrc Int,
tzFare Money,
tzAvgFare Money
)
CREATE INDEX [IX_#YBReport] ON [dbo].[#YBReport]([DeptCode]) ON [PRIMARY]'
End
Else
Begin
Set @CreateStr='Create Table #YBReport(
DeptCode varchar(20),
Sickid Int,
Visitid Int,
SickCode varchar(10),
SickName varchar(16),
Finance Char(2),
SumMoney Money,
tzFare Money,
tzAvgFare Money
)
CREATE INDEX [IX_#YBReport] ON [dbo].[#YBReport]([DeptCode],[Sickid],[VisitID]) ON [PRIMARY]'
End

Exec(@CreateStr)

Select * from #YBReport

truncate table #YBReport
drop table #YBReport

go

Exec up_xxx 0

34,590

社区成员

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

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