declare cur1 cursor for
select ... from table_name1 where ...group by / order by ...
open cur1
FETCH NEXT FROM cur1 INTO ...
while (@@FETCH_STATUS = 0 )
begin
....
declare cur2 cursor for
select ... from table_name2 where ...group by / order by ...
set @sql='select ... from table_name2 where ...group by / order by ...'
exec @sql
open cur2
FETCH NEXT FROM cur2 INTO ...
while (@@FETCH_STATUS = 0 )
begin
....
FETCH NEXT FROM cur2 INTO ...
end
....
close cur2
DEALLOCATE cur2
FETCH NEXT FROM cur1 INTO ...
end
close cur1
deallocate cur1