首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

变量赋值解决方法

2012-01-28 
变量赋值我用这个方法set@command( selecttop1compareletterfromdictionarywhereidnotin(selecttop+conve

变量赋值
我用这个方法
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

热点排行