这个存储过程错在哪儿?

OmT 2004-11-22 02:54:27
对储存过程不熟,照着写了一个存储过程,如下:
create proc abc
@currentpage int=1
as
select top 15 * from TestTable
where (id>(select Max(id) from(select top 15*@currentpage id from TestTable order by id) as T))
go

分析时提示:
服务器: 消息 170,级别 15,状态 1,过程 abc,行 5
第 5 行: '@currentpage' 附近有语法错误。

如果把15*@currentpage换成一个具体的数字就不会出错!
...全文
125 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
iuhxq 2004-11-22
  • 打赏
  • 举报
回复
declare
@str nvarchar(100),
@i int,
@j int
set @j=2
set @i=2*@j
set @str='select top '+str(@i)+' * from table1'
exec sp_executesql @str
OmT 2004-11-22
  • 打赏
  • 举报
回复
OK了:)改来改去改成下面的结果:
CREATE proc abc
@currentpage int=1
as
declare @top int
set @top=15*@currentpage
exec('select top 15 * from TestTable where (id>(select Max(id) from(select top '+@top+' id from TestTable order by id) as T))')
GO


谢谢各位大侠的回复,今天又学一招!嘿嘿
huangsuipeng 2004-11-22
  • 打赏
  • 举报
回复
漏了一个+
exec("select top 15 * where (id>(select max(id) from (select top"+ @topcount +"id from TestTable order by id)as T))")
huangsuipeng 2004-11-22
  • 打赏
  • 举报
回复
top不支持变量

用 exec("select top 15 * where (id>(select max(id) from (select top"+ @topcount "id from TestTable order by id)as T))")
我无验证过,但问题是需要这样解决的,就是以字符窜形式执行SELECT命令
OmT 2004-11-22
  • 打赏
  • 举报
回复
create proc abc1
@currentpage int=1
as
declare @topcount int
set @topcount=15*@currentpage
select top 15 * where (id>(select max(id) from (select top @topcount id from TestTable order by id)as T))
GO

第6行:“@topcount”附近错误

:(
xzq686 2004-11-22
  • 打赏
  • 举报
回复
下面那样试试:
create proc abc
@currentpage int=1,
@topcount int
as
set @topcount=15*@currentpage

select top 15 * from TestTable
where (id>(select Max(id) from(select top @topcount id from TestTable order by id) as T))
go
OmT 2004-11-22
  • 打赏
  • 举报
回复
明白一点了,但写的还是通不过,有烦哪位大侠把上面的重写一下好吗?谢谢!
berlin8600 2004-11-22
  • 打赏
  • 举报
回复
15*@@currentpage
北京的雾霾天 2004-11-22
  • 打赏
  • 举报
回复
top不支持变量吧
54989875 2004-11-22
  • 打赏
  • 举报
回复
declare @currentpage varchar(1000)
声明变量
shyboywjy0227 2004-11-22
  • 打赏
  • 举报
回复
SQL语句中是不支持数字乘以一个变量的,所以15*@currentpage这样肯定出错,
declare @sql varchar(1000)
declare @top int
set @top =15*@currentpage
set @sql='select top '+@top+' id from TestTable order by id'
exec (@sql)
上面的意思就是把需要变量的top的SQL语句变成字符串,然后exec (@sql)执行这个字符串

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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