向SP中传递表变量的一个错误,提示Must declare the scalar variable。

屁屁 2011-02-21 05:03:02
正在学习向Sp中传递 表变量。遇到一个错误:

create type dbo.TableType as table([numIndex] int not null primary key)

已经创建成功,从对象管理器中可以看到。

create procedure [dbo].[simpleTableType](@NumIndex dbo.TableType readonly)
as
begin
select * from aTable inner join @NumIndex on aTable.anum=@NumIndex.[numIndex]
end;

错误:
Msg 137, Level 16, State 1, Procedure simpleTableType, Line 4
Must declare the scalar variable "@NumIndex".

不知道这个错误 怎么改正。请大家帮忙。
...全文
396 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
王向飞 2011-02-21
  • 打赏
  • 举报
回复
select * from aTable inner join @NumIndex on aTable.anum=@NumIndex.[numIndex]

改为

select * from aTable inner join @NumIndex t on aTable.anum=t.[numIndex]
AcHerat 元老 2011-02-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 soft_fair 的回复:]
不要用动态sql,因为刚好参看书本学习 t-sql,按照书上的来,还是出现这种错误,就不知道为什么原因。
sql 2008 的sp 支持表变量。
[/Quote]

2008 ???等楼下高手,我2005!
屁屁 2011-02-21
  • 打赏
  • 举报
回复
不要用动态sql,因为刚好参看书本学习 t-sql,按照书上的来,还是出现这种错误,就不知道为什么原因。
sql 2008 的sp 支持表变量。
AcHerat 元老 2011-02-21
  • 打赏
  • 举报
回复

create procedure [dbo].[simpleTableType](@NumIndex varchar(100))
as
begin
exec('select * from aTable inner join ' + @NumIndex + ' on aTable.anum=' + @NumIndex +'.[numIndex]')
end

AcHerat 元老 2011-02-21
  • 打赏
  • 举报
回复

create procedure [dbo].[simpleTableType](@NumIndex varchar(100))
as
begin
exec('select * from aTable inner join' + @NumIndex + ' on aTable.anum=' + @NumIndex +'.[numIndex]')
end

34,591

社区成员

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

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