SELECT A1Org.orgId INTO #Data FROM A1Org WHERE ParentId='50b921a6-bdfc-401c-aa18-069ddbd38e4a' AND OrgNature =20 declare c_temp cursor --声明游标 for(select orgId from #Data) --循环的条件 open c_temp --打开游标 declare @orgId varchar(36) FETCH NEXT FROM c_temp into @orgId --移动游标指向到第一条数据,提取第一条数据存放在变量中 while @@fetch_status=0 --//如果上一次操作成功则继续循环 begin select count(*) from A1Org left join #Data on A1Org.OrgId=#Data.orgId WHERE A1Org.OrgNature=30 and A1Org.LevelCode LIKE +'%'+@orgId+'%' --print 'Org=='+@orgId FETCH NEXT FROM c_temp into @orgId --继续提取下一行数据 end close c_temp --关闭游标 deallocate c_temp --释放资源
运行结果图。