sqlserver游标循环换成while循环结果集

if(exists (select * from sys.objects where name = 'procName'))
drop proc procName
go
Create proc [dbo].[procName](@resultId int)
AS
    DECLARE @fact int=0,@other int=0,@asId int=0,@code varchar(20)='',@code1 varchar(20)='',@objCode varchar(20)='',@title nvarchar(255)='',@contract nvarchar(255)=''

    set @objCode=(select so.objCode from Result r join SurveyObj so on r.surveyObjId=so.surveyObjId where r.resultId = @resultId )
    declare  @cid int=0
    select top 1 @cid = cid from CounterMap order by cid
    while @cid is not null
    begin
        --用查询的方式赋值
        select @asId=answerId,@code=counterCode,@code1=counterCode1,@title=answerTitle  from CounterMap where cid=@cid

        set @fact=(select result from ResultDtl where answerId = @asId and questionId=21700 and  resultId = @resultId)
        set @other=(select sum(hardCoreNum) from hardcore_HW where objCode = @objCode and hardCoreCode in(select str2table from StrToTable(@contract)))
        --原理是每一次取最上面的一条数据进行循环直到结束
        set @cid = (
            select top 1 cid from CounterMap where cid>@cid  order by cid
        )
    end
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值