sqlserver 动态游标

--创建动态游标
set @sql = 'declare rowCursor cursor For select aimg,aname,aprice from ( select *, ROW_NUMBER() over('+ @order +') as rn from (select id,aimg,aname,aprice from F_Anli where '+ @where +') t ) a where rn>='+@start+' and rn<='+@end;
EXEC(@sql)
    
--打开游标
OPEN rowCursor; 
--取值
fetch next from rowCursor into @aimg,@aname,@aprice;
--循环
while(@@FETCH_STATUS=0)
begin
set @eachjson = @eachjson+'<row><aimg>'+@aimg+'</aimg>'+'<aname>'+@aname+'</aname>'+'<aprice>'+@aprice+'</aprice></row>';
fetch next from rowCursor into @aimg,@aname,@aprice;
end; 
--关闭游标
close rowCursor;
--释放游标控件
deallocate rowCursor;

猜你喜欢

转载自blog.csdn.net/knuthz/article/details/80784970