当前位置: 代码迷 >> Sql Server >> 变量赋值解决方法
  详细解决方案

变量赋值解决方法

热度:84   发布时间:2016-04-27 18:50:06.0
变量赋值
我用这个方法
set   @command=( 'select   top   1   compareletter   from   dictionary   where   id   not   in(select   top   '+convert(nvarchar,@ma)+ '   id   from   dictionary) ')
exec   sp_executesql   @command,N '@exchange       nvarchar       output ',@exchange   output
[email protected];但是为什么   @exchange   没有传出来值   查询的时候是   null   怎么办??

------解决方案--------------------
在你的动态SQL语句中根本就没有变量,哪来的输出?

试着这么改一下:

set @command=( 'select top 1 @exchange=compareletter from dictionary where id not in(select top '+convert(nvarchar,@ma)+ ' id from dictionary) ')

exec sp_executesql @command,N '@exchange nvarchar output ',@exchange output
------解决方案--------------------
declare @ma int
declare @exchange nvarchar(200)
declare @command nvarchar(500)
set @ma=0
while (@ma <(select count(compareletter) from dictionary))
begin
set @command=( 'select top 1 @exchange=compareletter from dictionary where id not in(select top '+convert(nvarchar,@ma)+ ' id from dictionary) ')
exec sp_executesql @command,N '@exchange nvarchar(200) output ',@exchange output
select @exchange as '替换内容 '
set @[email protected]+1
end
select [content] from messageboard
  相关解决方案