日期:2014-05-18  浏览次数:20481 次

变量赋值
我用这个方法
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
给@exchange赋值;但是为什么   @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 @ma=@ma+1
end
select [content] from messageboard