declare @name nvarchar(50);declare @id nvarchar(50)
declare pro_cur cursor for select id,name from T1
open pro_cur
fetch next from pro_cur into @id,@name
while (@@fetch_status = 0)
begin
---------
print @name
----遍历城市
declare ccur cursor for select cityid,cityName from T2 WHERE T2.T1_ID=@id
open ccur
fetch next from ccur into @id,@name
while(@@fetch_status=0)
begin
print (@name)
fetch next from ccur into @id,@name
end
close ccur
deallocate ccur
---------
fetch next from pro_cur into @id,@name
end
close pro_cur
deallocate pro_cur
SQL使用游标遍历多层table获取每行的数据进行操作
最新推荐文章于 2021-01-24 03:49:58 发布