sqlserver 存储过程循环遍历结果集,执行相应操作

 

sqlserver 存储过程循环遍历结果集,执行相应操作

--循环学校表更新每个学校的学校区镇、学段、学校名称
DECLARE @sql NVARCHAR(MAX),@schoolId NVARCHAR(50),@tableName NVARCHAR(150)
DECLARE rs CURSOR LOCAL SCROLL FOR
select DISTINCT XXDM from dbo.C_GB_SchoolInfo WITH(NOLOCK)
OPEN rs
FETCH NEXT FROM rs INTO @schoolId
WHILE @@FETCH_STATUS = 0
BEGIN
    IF(@schoolId!=''AND @schoolId IS NOT null)
    BEGIN
            SET @tableName= 'Service_PlatformStatistics_'+REPLACE(@schoolId,'-','_')
            IF EXISTS(SELECT 1 FROM dbo.SysObjects WHERE name=@tableName)
                BEGIN
                    
                    --这里写业务代码
                END 
    END
FETCH NEXT FROM rs INTO @schoolId
END
CLOSE rs

 

posted @ 2020-12-10 17:04  apegu  阅读(4192)  评论(0编辑  收藏  举报