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

ms-sql数据库变量和top之间的组合问题
declare @num int
declare @ind int
declare @name int
set @ind=0
select @num=count(*) from 职工 where 仓库号 in('wh1','wh4')
while(@num>0)
begin
exec('select top 1 职工ID as ' + @name + ' from 职工 where 仓库号 in(''wh1'',''wh4'') and 职工ID not in(select top ' + @ind + ' 职工ID from 职工 where 仓库号 in(''wh1'',''wh4''))')
set @ind = @ind+1
set @num=@num-1
end
看下面这一语句出现问题了,拜求~
exec('select top 1 职工ID as '+@name+' from 职工')


------解决方案--------------------

SQL code
declare @n int, @sql nvarchar(400) 
set @sql='select top 1 @a=id from tb' 
exec sp_executesql @sql,N'@a int output',@n output 
select @n