光标设置操作

时间:2019-05-09 20:46:13

标签: sql sql-server cursor

我正在尝试将基于SQL Server游标的过程转换为SET操作。该过程是在游标内部在其他条件下设置变量,并使用这些值来插入和更新数据。这里使用了2个游标。以嵌套方式使用了游标。 非常感谢您的帮助。可能有多个


`IF (@MAINT_SW_APP_SERV_ENGINEERING_LMDW !=0) 
                BEGIN        

                    SET @ASSET_CATEGORY='Maintenance - Software - Applications (Server/Utility) - LMDW'
                    SET @ASSET_CATEGORY_COL='MAINT_SW_APP_SERV_ENGINEERING_LMDW'

                    DELETE FROM T_CT_REQUEST_ASSET_TOWER WHERE REQUEST_ID=@REQ_ID AND REQUEST_EST_ID=@REQ_EST AND EST_ID=@EST_ID AND ASSET_CATEGORY_COL=@ASSET_CATEGORY_COL

                    INSERT INTO T_CT_REQUEST_ASSET_TOWER VALUES(@REQ_ID, @REQ_EST ,@EST_ID, @ASSET_CATEGORY, @ASSET_CATEGORY_COL, 'LMDW', @MAINT_SW_APP_SERV_ENGINEERING_LMDW)
                END'

代码遍历整个请求表以获取请求ID。对于每个请求ID,它正在获取Hardware Mainframe Midrange,Storage等的值。根据变量的类别设置值。接下来,根据变量值,完成插入和更新。


下面的代码

SET @SQLAPND = N'SELECT DISTINCT REQUEST_ID FROM T_CT_TEMP_ADHOC_NEW'



        --1st Cursor for @REQ_ID
        SET @SQLCUR = 'DECLARE FetchReqId CURSOR READ_ONLY FOR '+@SQLAPND
        EXEC (@SQLCUR)

            OPEN FetchReqId 
            FETCH NEXT FROM FetchReqId INTO @REQ_ID

                SELECT E.REQUEST_EST_ID,COUNTRY,REGION,HW_MAINFRAME_MIDRANGE,HW_STORAGE, EST_ID
                    FROM T_CT_ESTIMATION E ,T_CT_REQUEST_ESTIMATION R  
                    WHERE R.REQUEST_EST_ID=E.REQUEST_EST_ID 
                    and R.REQUEST_ID = @REQ_ID

                        OPEN Fetch_Details 
                        FETCH NEXT FROM Fetch_Details
                        INTO @REQ_EST,@Country,@REG,@CONV_RT,@HW_MAINFRAME_MIDRANGE,@HW_STORAGE
                        WHILE @@ROWCOUNT <> 0
                        BEGIN

                            IF (@HW_MAINFRAME_MIDRANGE !=0 and (@HW_MAINFRAME_MIDRANGE>0 or @HW_MAINFRAME_MIDRANGE<0))
                            BEGIN
                                SET @ASSET_CATEGORY='Hardware - Mainframe/Midrange'
                                SET @TEMP_DATA=@HW_MAINFRAME_MIDRANGE
                                SET @COST_COL='HW_MAINFRAME_MIDRANGE'
                            END


                            IF (@HW_STORAGE !=0 and (@HW_STORAGE>@TEMP_DATA))
                            BEGIN
                                SET @ASSET_CATEGORY='Hardware - Storage'
                                SET @TEMP_DATA=@HW_STORAGE
                                SET @COST_COL='HW_STORAGE'
                            END

                        FETCH NEXT FROM Fetch_Details
                        INTO @REQ_EST,@Country,@REG,@CONV_RT,@HW_MAINFRAME_MIDRANGE,@HW_STORAGE,@EST_ID             
                        END

                        SET @sql = N'SELECT @TOWER=TOWER FROM T_CT_LOOKUP_ASSET_TOWER_MAPPING WHERE ASSET_CATEGORIES='''+@ASSET_CATEGORY+''''
                        EXEC sp_executesql



                        IF (@MAINT_SW_APP_SERV_ENGINEERING_LMDW !=0) 
                    BEGIN        

                        SET @ASSET_CATEGORY='Maintenance - Software - Applications (Server/Utility) - LMDW'
                        SET @ASSET_CATEGORY_COL='MAINT_SW_APP_SERV_ENGINEERING_LMDW'

                        DELETE FROM T_CT_REQUEST_ASSET_TOWER WHERE REQUEST_ID=@REQ_ID AND REQUEST_EST_ID=@REQ_EST AND EST_ID=@EST_ID AND ASSET_CATEGORY_COL=@ASSET_CATEGORY_COL

                        INSERT INTO T_CT_REQUEST_ASSET_TOWER VALUES(@REQ_ID, @REQ_EST ,@EST_ID, @ASSET_CATEGORY, @ASSET_CATEGORY_COL, 'LMDW', @MAINT_SW_APP_SERV_ENGINEERING_LMDW)
                    END



                CLOSE Fetch_Details
                DEALLOCATE Fetch_Details

        FETCH NEXT FROM FetchReqId
        INTO @REQ_ID
        END



    CLOSE FetchReqId
    DEALLOCATE FetchReqId

0 个答案:

没有答案