读书人

变量赋值解决方法

发布时间: 2012-01-28 22:06:13 作者: rapoo

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

读书人网 >SQL Server

热点推荐