无法将游标UPDATE查询转换为SQL Server中的SET操作

时间:2018-03-14 19:08:26

标签: sql-server set

过程正在使用此结构的嵌套游标

OPEN FetchReqId
   OPEN FetchYear
      OPEN FetchCountry
      CLOSE FetchCountry  
   CLOSE FetchYear   
   OPEN FetchRegion
   CLOSE FetchRegion  
CLOSE FetchReqId

请查看程序的一部分

DECLARE FetchCountry CURSOR READ_ONLY FOR
    SELECT DISTINCT COUNTRY 
    FROM T_CT_ESTIMATION 
    WHERE REQUEST_EST_ID = @REQUEST_EST_ID 
    ORDER BY COUNTRY

                        OPEN FetchCountry
                        FETCH NEXT FROM FetchCountry INTO @COUNTRY_TEMP
                        WHILE @@ROWCOUNT <> 0
                            BEGIN
                                --PRINT 'FOR A PARTICULAR COUNTRY:::::::::::::::::::::::::::::::::::' PRINT @COUNTRY_TEMP
                                --SET @FA_BASE_EXP=0.00
                                --SET @INTVST_EXP=0.00

                                DECLARE FetchEstId CURSOR READ_ONLY FOR
                                    SELECT DISTINCT EST_ID FROM T_CT_ESTIMATION WHERE REQUEST_EST_ID=@REQUEST_EST_ID AND COUNTRY=@COUNTRY_TEMP
                                OPEN FetchEstId
                                FETCH NEXT FROM FetchEstId INTO @EST_ID
                                WHILE @@ROWCOUNT <> 0
                                    BEGIN
                                        --PRINT 'FOR A PARTICULAR EST ID ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::'
                                        PRINT @EST_ID
                                        select @INTVST_EXP_TO_ADD=SUM(ROUND(((inner1.cont*inner1.invstExp)+inner1.taxPer+inner1.invstExp)*inner1.CONVERSION_RATE,0))
                                        from
                                        (select ((case when e.MAINT_HW_MAINFRAME is null then 0 else MAINT_HW_MAINFRAME end)+(case when e.MAINT_HW_STORAGE is null then 0 else MAINT_HW_STORAGE end)+(case when e.MAINT_HW_SERV_WINDWS is null then 0 else MAINT_HW_SERV_WINDWS end)+(case when e.MAINT_HW_SERV_UNIX is null then 0 else MAINT_HW_SERV_UNIX end)+(case when e.MAINT_HW_SERV_LINUX is null then 0 else MAINT_HW_SERV_LINUX end)+(case when e.MAINT_HW_PC is null then 0 else MAINT_HW_PC end)+
                                        ISNULL(MAINT_HW_OTHER_DATACNTR,0)+(case when e.MAINT_SW_APP_SERV_OTHER is null then 0 else e.MAINT_SW_APP_SERV_OTHER end)+
                                        (case when e.MAINT_SW_APP_SERV_DATA is null then 0 else e.MAINT_SW_APP_SERV_DATA end)+(case when e.MAINT_SW_CLIENT_COMP is null then 0 else e.MAINT_SW_CLIENT_COMP end)+
                                        (case when e.MAINT_SW_APP_SERV_ENGINEERING is null then 0 else e.MAINT_SW_APP_SERV_ENGINEERING end)+
                                        ISNULL(EXP_CONTRACT_CONSULT,0)+ISNULL(EXP_INTERNAL_LABOUR,0)+ISNULL(CROS,0)+ISNULL(MACD,0)+ISNULL(PROF_SERVICES,0)+ISNULL(OTHER_EXP_ELEMENT,0)) as invstExp,
                                        EXP_CONTI_PER/100 as cont,
                                        ((EXP_SALE_TAX_VAT/100)*(ISNULL(MAINT_HW_MAINFRAME,0)+ISNULL(MAINT_HW_STORAGE,0)+ISNULL(MAINT_HW_SERV_WINDWS,0)+ISNULL(MAINT_HW_SERV_UNIX,0)+ISNULL(MAINT_HW_SERV_LINUX,0)+ISNULL(MAINT_HW_PC,0)+
                                        ISNULL(MAINT_HW_OTHER_DATACNTR,0)+(case when e.MAINT_SW_APP_SERV_OTHER is null then 0 else e.MAINT_SW_APP_SERV_OTHER end)+
                                        (case when e.MAINT_SW_APP_SERV_DATA is null then 0 else e.MAINT_SW_APP_SERV_DATA end)+(case when e.MAINT_SW_CLIENT_COMP is null then 0 else e.MAINT_SW_CLIENT_COMP end)+
                                        (case when e.MAINT_SW_APP_SERV_ENGINEERING is null then 0 else e.MAINT_SW_APP_SERV_ENGINEERING end)+ISNULL(CROS,0)+ISNULL(OTHER_EXP_ELEMENT,0))) as taxPer,
                                        CONVERSION_RATE,e.COUNTRY
                                        from T_CT_REQUEST_ESTIMATION r,T_CT_ESTIMATION e 
                                        where r.REQUEST_EST_ID=e.REQUEST_EST_ID and Year=@YR and COUNTRY=@COUNTRY_TEMP and r.REQUEST_ID= @REQID and e.EST_ID=@EST_ID) inner1    

                                        IF(@INTVST_EXP_TO_ADD = NULL)
                                        BEGIN
                                            SELECT @INTVST_EXP_TO_ADD = 0.00
                                        END

                                        select @FA_BASE_EXP_TO_ADD=SUM(ROUND((inner1.totFaBase+inner1.contPer+inner1.taxPer)*inner1.CONVERSION_RATE,0))
                                        from
                                        (select (ISNULL(MAINT_HW_DISTR_LAN,0)+ISNULL(MAINT_HW_WAN_NTWRK,0)+ISNULL(MAINT_HW_TELECOM,0)+ISNULL(MAINT_HW_OTHER_NTWRK,0)+(case when e.MAINT_SW_APP_SERV_NTWRK is null then 0 else e.MAINT_SW_APP_SERV_NTWRK end)+isnull(CIRCUITS,0)) as totFaBase,
                                        (ISNULL(MAINT_HW_DISTR_LAN,0)+ISNULL(MAINT_HW_WAN_NTWRK,0)+ISNULL(MAINT_HW_TELECOM,0)+ISNULL(MAINT_HW_OTHER_NTWRK,0)+(case when e.MAINT_SW_APP_SERV_NTWRK is null then 0 else e.MAINT_SW_APP_SERV_NTWRK end)+ISNULL(CIRCUITS,0) )*(ISNULL(EXP_CONTI_PER,0)/100) as contPer,
                                        (ISNULL(MAINT_HW_DISTR_LAN,0)+ISNULL(MAINT_HW_WAN_NTWRK,0)+ISNULL(MAINT_HW_TELECOM,0)+ISNULL(MAINT_HW_OTHER_NTWRK,0)+(case when e.MAINT_SW_APP_SERV_NTWRK is null then 0 else e.MAINT_SW_APP_SERV_NTWRK end)+ISNULL(CIRCUITS,0) )*(ISNULL(EXP_SALE_TAX_VAT,0)/100) as taxPer,
                                        CONVERSION_RATE,COUNTRY
                                        from T_CT_REQUEST_ESTIMATION r,T_CT_ESTIMATION e 
                                        where r.REQUEST_EST_ID=e.REQUEST_EST_ID and Year=@YR and COUNTRY=@COUNTRY_TEMP and r.REQUEST_ID=@REQID and e.EST_ID=@EST_ID) inner1 

                                        IF(@FA_BASE_EXP_TO_ADD = NULL)
                                        BEGIN
                                            SELECT @FA_BASE_EXP_TO_ADD = 0.00
                                        END

                                        IF(@COUNTRY_TEMP='US' OR @COUNTRY_TEMP='BELGIUM')
                                        BEGIN
                                            SET @INTVST_EXP= @INTVST_EXP+ @INTVST_EXP_TO_ADD
                                            SET @FA_BASE_EXP=@FA_BASE_EXP+@FA_BASE_EXP_TO_ADD
                                            PRINT 'YES COUNTRY IS EITHER US OR BELGIUM:::: ' PRINT @INTVST_EXP PRINT @FA_BASE_EXP
                                        END
                                        ELSE
                                        BEGIN
                                            select @FA_BASE_EXP_TO_ADD=SUM((TOTAL_EXP*CONVERSION_RATE)) from T_CT_REQUEST_ESTIMATION r,T_CT_ESTIMATION e
                                            where r.REQUEST_EST_ID=e.REQUEST_EST_ID and YEAR=@YR
                                            and COUNTRY=@COUNTRY_TEMP
                                            and REQUEST_ID=@REQID
                                            and e.EST_ID=@EST_ID

                                            SET @FA_BASE_EXP=@FA_BASE_EXP+@FA_BASE_EXP_TO_ADD
                                            PRINT '@INTVST_EXP::::: for Country Other than US and Belgium' PRINT @INTVST_EXP
                                            PRINT '@FA_BASE_EXP:::::' PRINT @FA_BASE_EXP
                                        END

                                        --PRINT 'INVEST EXPENSE TO ADD:::::::::' PRINT @INTVST_EXP_TO_ADD
                                        --PRINT 'FA BASE EXPENSE TO ADD:::::::::' PRINT @FA_BASE_EXP_TO_ADD

                                        --PRINT 'FOR A PARTICULAR EST ID ENDED====================================================='                                        
                                    FETCH NEXT FROM FetchEstId
                                    INTO @EST_ID
                                    END 

                                CLOSE FetchEstId
                                DEALLOCATE FetchEstId

                                --PRINT 'FOR A PARTICULAR COUNTRY END================================================='
                            FETCH NEXT FROM FetchCountry
                            INTO @COUNTRY_TEMP
                            END

                        CLOSE FetchCountry
                        DEALLOCATE FetchCountry

SET @SQL =&#39;&#39;

                        /*SET @SQL = N'UPDATE T_CT_TEMP_ADHOC SET Year'+cast(@Countr as varchar(10))+'_Capital = '+cast(@USD_TOT_CAP as varchar(1000))+',Year'+cast(@Countr as varchar(10))+'_Expense = '+cast(@USD_TOT_EXP as varchar(1000))+' ,Year'+cast(@Countr as varchar(10))+'='+@YR+'WHERE ID = ' +@SESSNID+' AND REQUEST_ID = '''+@REQID+''''*/


                        --TO CHANGE UPDATE QUERY AND ADD FA BAse EXP and INvestMent Exp for All years as splitted. 
                        --Also create Variables in Code and get it to display in Report Page
                        SET @SQL = N'UPDATE T_CT_TEMP_ADHOC SET Year'+cast(@Countr as varchar(10))+'_Capital = '+cast(@USD_TOT_CAP as varchar(1000))+
                                    ',Year'+cast(@Countr as varchar(10))+'_Expense = '+cast(@USD_TOT_EXP as varchar(1000))+
                                    ',Year'+cast(@Countr as varchar(10))+'_ONG_EXP_MAINT_HW = '+cast(@USD_ONG_HW as varchar(1000))+
                                    ',Year'+cast(@Countr as varchar(10))+'_ONG_EXP_MAINT_SW = '+cast(@USD_ONG_SW as varchar(1000))+
                                    ',Year'+cast(@Countr as varchar(10))+'_ONG_EXP_EXPENSED_CONTRACT_CONSULT = '+cast(@USD_ONG_EXP_CONT_CONSLT as varchar(1000))+
                                    ',Year'+cast(@Countr as varchar(10))+'_ONG_EXP_EXPENSED_INTER_LABOUR = '+cast(@USD_ONG_EXP_INTRNL_LBR as varchar(1000))+
                                    ',Year'+cast(@Countr as varchar(10))+'_ONG_EXP_CROS = '+cast(@USD_ONG_EXP_CROS as varchar(1000))+
                                    ',Year'+cast(@Countr as varchar(10))+'_ONG_EXP_CIRCUITS = '+cast(@USD_ONG_EXP_CIRCTS as varchar(1000))+
                                    ',Year'+cast(@Countr as varchar(10))+'_ONG_EXP_SALE_AMNT = '+cast(@USD_ONG_EXP_SALES_AMOUNT as varchar(1000))+
                                    ',Year'+cast(@Countr as varchar(10))+'='+@YR+
                                    ',Year'+cast(@Countr as varchar(10))+'_FA_BASE_SPLIT_EXP='+cast(@FA_BASE_EXP as varchar(1000))+
                                    ',Year'+cast(@Countr as varchar(10))+'_INVST_SPLIT_EXP='+cast(@INTVST_EXP as varchar(1000))+
                                    ' WHERE ID = ' +@SESSNID+' AND REQUEST_ID = '''+@REQID+''''


                        print 'FFF'
                        PRINT @SQL

                        EXEC sp_executesql
                        @query = @SQL

                        SET @Countr = @Countr+1
                        SET @FA_BASE_EXP=0
                        SET @INTVST_EXP=0
                        SET @FA_BASE_EXP_TO_ADD=0

                FETCH NEXT FROM FetchYear INTO @YR,@REQUEST_EST_ID
                END 
                CLOSE FetchYear
                DEALLOCATE FetchYear  

现在我将一个查询修改为

    select 

  SUM(ROUND(((inner1.cont*inner1.invstExp)+inner1.taxPer+inner1.invstExp)*inner1.CONVERSION_RATE,0))
                                            from
                                            (select inner1.EST_ID,((case when e.MAINT_HW_MAINFRAME is null then 0 else MAINT_HW_MAINFRAME end)+(case when e.MAINT_HW_STORAGE is null then 0 else MAINT_HW_STORAGE end)+(case when e.MAINT_HW_SERV_WINDWS is null then 0 else MAINT_HW_SERV_WINDWS end)+(case when e.MAINT_HW_SERV_UNIX is null then 0 else MAINT_HW_SERV_UNIX end)+(case when e.MAINT_HW_SERV_LINUX is null then 0 else MAINT_HW_SERV_LINUX end)+(case when e.MAINT_HW_PC is null then 0 else MAINT_HW_PC end)+
                                            ISNULL(MAINT_HW_OTHER_DATACNTR,0)+(case when e.MAINT_SW_APP_SERV_OTHER is null then 0 else e.MAINT_SW_APP_SERV_OTHER end)+
                                            (case when e.MAINT_SW_APP_SERV_DATA is null then 0 else e.MAINT_SW_APP_SERV_DATA end)+(case when e.MAINT_SW_CLIENT_COMP is null then 0 else e.MAINT_SW_CLIENT_COMP end)+
                                            (case when e.MAINT_SW_APP_SERV_ENGINEERING is null then 0 else e.MAINT_SW_APP_SERV_ENGINEERING end)+
                                            ISNULL(EXP_CONTRACT_CONSULT,0)+ISNULL(EXP_INTERNAL_LABOUR,0)+ISNULL(CROS,0)+ISNULL(MACD,0)+ISNULL(PROF_SERVICES,0)+ISNULL(OTHER_EXP_ELEMENT,0)) as invstExp,
                                            EXP_CONTI_PER/100 as cont,
                                            ((EXP_SALE_TAX_VAT/100)*(ISNULL(MAINT_HW_MAINFRAME,0)+ISNULL(MAINT_HW_STORAGE,0)+ISNULL(MAINT_HW_SERV_WINDWS,0)+ISNULL(MAINT_HW_SERV_UNIX,0)+ISNULL(MAINT_HW_SERV_LINUX,0)+ISNULL(MAINT_HW_PC,0)+
                                            ISNULL(MAINT_HW_OTHER_DATACNTR,0)+(case when e.MAINT_SW_APP_SERV_OTHER is null then 0 else e.MAINT_SW_APP_SERV_OTHER end)+
                                            (case when e.MAINT_SW_APP_SERV_DATA is null then 0 else e.MAINT_SW_APP_SERV_DATA end)+(case when e.MAINT_SW_CLIENT_COMP is null then 0 else e.MAINT_SW_CLIENT_COMP end)+
                                            (case when e.MAINT_SW_APP_SERV_ENGINEERING is null then 0 else e.MAINT_SW_APP_SERV_ENGINEERING end)+ISNULL(CROS,0)+ISNULL(OTHER_EXP_ELEMENT,0))) as taxPer,
                                            CONVERSION_RATE,e.COUNTRY
                                            from T_CT_REQUEST_ESTIMATION r,T_CT_ESTIMATION  e group by e.EST_ID
                                            where r.REQUEST_EST_ID=e.REQUEST_EST_ID and Year=@YR and COUNTRY=@COUNTRY_TEMP and r.REQUEST_ID= @REQID and e.EST_ID in (** SELECT DISTINCT EST_ID FROM T_CT_ESTIMATION WHERE REQUEST_EST_ID=@REQUEST_EST_ID AND COUNTRY=@COUNTRY_TEMP**)) inner1 group by EST_ID inner1  

所以基本上我试图用select in语句替换游标 但是我无法理解如何替换@SQL update语句,因为它在通过动态计算接收到值后逐行更新数据。
如何批量完成上传数据?

0 个答案:

没有答案