用表变量替换CURSOR

时间:2018-10-05 03:28:09

标签: sql-server tsql database-cursor

我不太擅长SQL。我已经看到以下存储过程需要花费超过15分钟的时间来执行。我读过应该避免使用CURSOR,但我不知道该怎么做。是否像CURSOR操作那样逐行循环遍历表变量很慢?请帮忙。

SET @GetStudents=CURSOR FOR SELECT nStudentInsessionID FROM GetStudentFullName WHERE cSession=@cSession AND nCampusID=@nCampusID AND nClassID IN (SELECT t1 From @t1) ORDER BY nStudentInsessionID
OPEN @GetStudents FETCH NEXT FROM @GetStudents INTO @nStudentInSessionID
 WHILE @@FETCH_STATUS=0
    BEGIN
     SET @GetDepositIn=CURSOR FOR SELECT t2 From @t2 ORDER BY t2
     OPEN @GetDepositIn FETCH NEXT FROM @GetDepositIn INTO @nDepositID
        WHILE @@FETCH_STATUS=0
         BEGIN
            SET @GetDepositInGroup=CURSOR FOR SELECT t3 From @t3 ORDER BY t3
            OPEN @GetDepositInGroup FETCH NEXT FROM @GetDepositInGroup INTO @tnDepositInGroupID
                WHILE @@FETCH_STATUS=0
                 BEGIN
                    SET @GetFeeHead=CURSOR FOR SELECT t4 From @t4 ORDER BY t4
                    OPEN @GetFeeHead FETCH NEXT FROM @GetFeeHead INTO @tnFeeHeadID
                        WHILE @@FETCH_STATUS=0
                            BEGIN
                                SELECT @nActualFee=ISNULL(SUM(nAmount),0) FROM FeeGroupDetail WHERE nFeeGroupDetailID IN(SELECT DISTINCT nFeeGroupDetailID FROM FeeStudentFeeGroupRelation WHERE nStudentInSessionID=@nStudentInSessionID  AND nDepositIngroupID IN(@tnDepositInGroupID) AND nDepositIN IN (@nDepositID) AND nFeeHeadID in (@tnFeeHeadID))
                                SELECT @nStudFee=ISNULL(SUM(nAmount),0) FROM FeeStudentFeeGroupRelation WHERE nStudentInSessionID=@nStudentInSessionID AND nDepositIN IN (@nDepositID) AND nDepositIngroupID IN(@tnDepositInGroupID) and nFeeHeadID in (@tnFeeHeadID)
                                IF @Ischeckdate=1
                                    BEGIN
                                        SELECT @nFeePaid=ISNULL(SUM(nFeePaid),0) FROM FeeDepositMaster INNER JOIN FeeDepositDetail ON FeeDepositMaster.nFeeDepositID=FeeDepositDetail.nFeeDepositID WHERE FeeDepositMaster.dFeeDepositDate<=@dDueUpto AND ISNULL(FeeDepositMaster.IsCanceled,0)=0 AND FeeDepositMaster.nStudentInSessionID=@nStudentInSessionID AND nDepositIN IN (@nDepositID) AND FeeDepositDetail.nFeeHeadID in (@tnFeeHeadID) AND nDepositIn IN (SELECT DISTINCT nDepositIn FROM FeeGroupDetail WHERE  nDepositInGroupID in (@tnDepositInGroupID))
                                        SELECT @nConcession=ISNULL(SUM(nConcession),0) FROM FeeDepositMaster INNER JOIN FeeDepositDetail ON FeeDepositMaster.nFeeDepositID=FeeDepositDetail.nFeeDepositID WHERE FeeDepositMaster.dFeeDepositDate<=@dDueUpto AND ISNULL(FeeDepositMaster.IsCanceled,0)=0 AND FeeDepositMaster.nStudentInSessionID=@nStudentInSessionID AND nDepositIN IN (@nDepositID) AND FeeDepositDetail.nFeeHeadID in (@tnFeeHeadID) AND nDepositIn IN (SELECT DISTINCT nDepositIn FROM FeeGroupDetail WHERE  nDepositInGroupID in (@tnDepositInGroupID))
                                        SELECT @nLateFee=ISNULL(SUM(nLateFeeAmount),0) FROM FeeDepositMaster INNER JOIN FeeDepositDetail ON FeeDepositMaster.nFeeDepositID=FeeDepositDetail.nFeeDepositID WHERE FeeDepositMaster.dFeeDepositDate<=@dDueUpto AND ISNULL(FeeDepositMaster.IsCanceled,0)=0 AND FeeDepositMaster.nStudentInSessionID=@nStudentInSessionID AND nDepositIN IN (@nDepositID) AND FeeDepositDetail.nFeeHeadID in (@tnFeeHeadID) AND nDepositIn IN (SELECT DISTINCT nDepositIn FROM FeeGroupDetail WHERE  nDepositInGroupID in (@tnDepositInGroupID))
                                    END
                                ELSE
                                    BEGIN
                                        SELECT @nFeePaid=ISNULL(SUM(nFeePaid),0) FROM FeeDepositMaster INNER JOIN FeeDepositDetail ON FeeDepositMaster.nFeeDepositID=FeeDepositDetail.nFeeDepositID WHERE ISNULL(FeeDepositMaster.IsCanceled,0)=0 AND FeeDepositMaster.nStudentInSessionID=@nStudentInSessionID AND nDepositIN IN (@nDepositID) AND FeeDepositDetail.nFeeHeadID in (@tnFeeHeadID) AND nDepositIn IN (SELECT DISTINCT nDepositIn FROM FeeGroupDetail WHERE  nDepositInGroupID in (@tnDepositInGroupID))
                                        SELECT @nConcession=ISNULL(SUM(nConcession),0) FROM FeeDepositMaster INNER JOIN FeeDepositDetail ON FeeDepositMaster.nFeeDepositID=FeeDepositDetail.nFeeDepositID WHERE ISNULL(FeeDepositMaster.IsCanceled,0)=0 AND FeeDepositMaster.nStudentInSessionID=@nStudentInSessionID AND nDepositIN IN (@nDepositID) AND FeeDepositDetail.nFeeHeadID in (@tnFeeHeadID) AND nDepositIn IN (SELECT DISTINCT nDepositIn FROM FeeGroupDetail WHERE  nDepositInGroupID in (@tnDepositInGroupID))
                                        SELECT @nLateFee=ISNULL(SUM(nLateFeeAmount),0) FROM FeeDepositMaster INNER JOIN FeeDepositDetail ON FeeDepositMaster.nFeeDepositID=FeeDepositDetail.nFeeDepositID WHERE ISNULL(FeeDepositMaster.IsCanceled,0)=0 AND FeeDepositMaster.nStudentInSessionID=@nStudentInSessionID AND nDepositIN IN (@nDepositID) AND FeeDepositDetail.nFeeHeadID in (@tnFeeHeadID) AND nDepositIn IN (SELECT DISTINCT nDepositIn FROM FeeGroupDetail WHERE  nDepositInGroupID in (@tnDepositInGroupID))    
                                    END 
                                    --=========================================================
                                INSERT INTO FeesDueFeeListHeadWise_Temp(nStudentInSessionID,nDepositInGroupID,nDepositIn,nFeeHeadID,nActualFee,nStudFee,nFeePaid,nConcession,nLateFee,nCampusID,cSession)
                                VALUES(@nStudentInSessionID,@tnDepositInGroupID,@nDepositID,@tnFeeHeadID,ISNULL(@nActualFee,0),ISNULL(@nStudFee,0),ISNULL(@nFeePaid,0),ISNULL(@nConcession,0),ISNULL(@nLateFee,0),@nCampusID,@cSession)                                 
                                FETCH NEXT FROM @GetFeeHead INTO @tnFeeHeadID                           
                            END
                    FETCH NEXT FROM @GetDepositInGroup INTO @tnDepositInGroupID     
                    CLOSE @GetFeeHead
                    DEALLOCATE @GetFeeHead                      
                 END                    
            FETCH NEXT FROM @GetDepositIn INTO @nDepositID                                   
            CLOSE @GetDepositInGroup
            DEALLOCATE @GetDepositInGroup
         END
        FETCH NEXT FROM @GetStudents INTO @nStudentInSessionID                  
        CLOSE @GetDepositIn
        DEALLOCATE @GetDepositIn
     END
 CLOSE @GetStudents
DEALLOCATE @GetStudents

0 个答案:

没有答案