存储过程中的sql语句,改成变量就不行了,详细请进

huzx 2003-08-25 11:51:03
我在存储过程中使用下面的语句可以
update table1 set id=@id where id_2 in (select top 2 id_2 from goods where col3 is null)

但把2改为一个变量@count就不行了

即:
update table1 set id=@id where id_2 in (select top @count id_2 from goods where col3 is null)

其中@count定义了,且在前面已经赋过值
...全文
48 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
huzx 2003-08-26
  • 打赏
  • 举报
回复
好了,yujohny(踏网无痕)的可以,给份!
yujohny 2003-08-26
  • 打赏
  • 举报
回复
主意top后加空格、id_2前加空格,我测试过这样写通过。
declare @count int
set @count = 2

EXEC('update table1 set id=@id where id_2 in (select top '+ @count+'
id_2 from table1 where col3 is null)')
yujohny 2003-08-26
  • 打赏
  • 举报
回复
declare @count int
set @count = 2

EXEC('update table1 set id=@id where id_2 in (select top'+ @count+'
id_2 from table1 where col3 is null)')
huzx 2003-08-25
  • 打赏
  • 举报
回复
按时大苏打
yangvxin1 2003-08-25
  • 打赏
  • 举报
回复

整成个字符串形式好象:

declare @pp varchar(200)

set @pp='update table1 set id='+cast(@id as varchar)+' where id_2 in (select top '+ cast(@count as varchar)+' id_2 from table1 where col3 is null) '

exec (@pp)


huzx 2003-08-25
  • 打赏
  • 举报
回复
我的心在等待永远在等待
huzx 2003-08-25
  • 打赏
  • 举报
回复
大家晓得是怎么回事吗?


我的心在等待永远在等待
我的心在等待永远在等待
我的心在等待永远在等待
我的心在等待永远在等待
我的心在等待永远在等待




huzx 2003-08-25
  • 打赏
  • 举报
回复
我在存储过程中使用下面的语句可以
update table1 set id=@id where id_2 in (select top 2 id_2 from table1 where col3 is null)

但把2改为一个变量@count就不行了

即:
update table1 set id=@id where id_2 in (select top @count id_2 from table1 where col3 is null)

其中@count定义了,且在前面已经赋过值
huzx 2003-08-25
  • 打赏
  • 举报
回复
谢谢两位,怎么还是不行呢?
txlicenhe 2003-08-25
  • 打赏
  • 举报
回复
declare @count int
set @count = 10
exec('update table1 set id=@id where id_2 in (select top ' + cast(@count as varchar )+ 'id_2 from goods where col3 is null)')

34,576

社区成员

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

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