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;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章