在存储过程中使用top

softwarewander 2006-06-22 10:55:47
我写了这样的语句
set @value=(select top @count-@index+1 bdamount from bomdetail where bmid=@product and mciid not in (select top @count-@index mciid from bomdetail where bmid=@product))
--保留路径的分叉点的数据环境


那些变量我都声明过 且都赋了值 但是同不过语法检查 为什么呢?




...全文
166 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
softwarewander 2006-06-22
  • 打赏
  • 举报
回复
恩 好了
现在我是这样写了
declare strsql varchar(2000)
declare @value varchar(100)
set @strsql="select top " +str(@count-@index+1) +" bdamount from bomdetail where bmid=" + @product +" and mciid not in (select top " + str(@count-@index)+" mciid from bomdetail where bmid=" +@product +"))"
下面我想把检索出的 那一条bdamount数据 赋值给另一个变量@value 怎么办呢?
我市这样写的, 但是有语法错误 ?
set @value=exec (@strsql)
LouisXIV 2006-06-22
  • 打赏
  • 举报
回复
TOP只支持常量

可以用动态SQL来拼接

点点星灯 2006-06-22
  • 打赏
  • 举报
回复
是,这样是不符合SQL的语法的,
首先是不可以用表达式@count-@index+1 ,必须是一个数字

如果top 后面是变量,必须用动态SQL语句
gohst001 2006-06-22
  • 打赏
  • 举报
回复
不能这样写,2000不支持这样的写法,top的数量要是常量

34,590

社区成员

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

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