我的sql有三个游标,我认为它太多了,是否有更好的方法?

时间:2019-06-28 12:49:19

标签: tsql

这是我的代码。在db中,对于每个主题,将有多个与外部课程等效的内部课程。外部与内部课程的关系是多对多的,例如,如果学生的转学成绩单上有前一所学校的两门课程BIOL 101、102和105,则该学校将在其中学习三门课程,并记录该学生在这门课程中选择了103和104 。所以我的脚本是生成报告,首先为每个关系列出外部课程,然后列出相应的内部课程。

我只是觉得我的脚本有太多的游标(其中三个)。有更好的方法吗?

代码如下:

DECLARE @SUBJECT VARCHAR(5), @SEQ VARCHAR(5);

DECLARE @SCHOOL_SUBJECT VARCHAR(5), @SCHOOL_CRSE_NBR VARCHAR(10), @CRSE_ID VARCHAR(10), @CRSE_OFFER_NBR VARCHAR(10), @COMP_SUBJECT_AREA VARCHAR(5), @BEN_CRSE_ID VARCHAR(10),@BEN_OFFER_NBR VARCHAR(10)

DECLARE SUBJ_SEQ_CURSOR CURSOR LOCAL FORWARD_ONLY READ_ONLY FOR
SELECT DISTINCT COMP_SUBJECT_AREA, TRNSFR_EQVLNCY_CMP
FROM PS_TRNSFR_COMP A
WHERE A.INSTITUTION='THEINSTITUTION' AND A.TRNSFR_SRC_ID='0222' AND A.EFFDT='2019-06-21'

OPEN SUBJ_SEQ_CURSOR
FETCH NEXT FROM SUBJ_SEQ_CURSOR INTO @SUBJECT, @SEQ

WHILE @@FETCH_STATUS=0 
BEGIN
    SELECT @SUBJECT, @SEQ 

    DECLARE DETAIL_CURSOR_FROM CURSOR LOCAL FORWARD_ONLY READ_ONLY FOR 
    SELECT B.SCHOOL_SUBJECT, B.SCHOOL_CRSE_NBR, B.CRSE_ID, B.CRSE_OFFER_NBR 
    FROM PS_TRNSFR_COMP A 
    INNER JOIN PS_TRNSFR_FROM B 
        ON A.INSTITUTION=B.INSTITUTION 
        AND A.TRNSFR_SRC_ID=B.TRNSFR_SRC_ID
        AND A.COMP_SUBJECT_AREA=B.COMP_SUBJECT_AREA
        AND A.TRNSFR_EQVLNCY_CMP=B.TRNSFR_EQVLNCY_CMP
        AND A.EFFDT=B.EFFDT
    WHERE A.INSTITUTION='THEINSTITUTION' AND A.TRNSFR_SRC_ID='0222' AND A.EFFDT='2019-06-21'
    AND A.TRNSFR_EQVLNCY_CMP=@SEQ
    AND A.COMP_SUBJECT_AREA=@SUBJECT

    OPEN DETAIL_CURSOR_FROM
    FETCH NEXT FROM DETAIL_CURSOR_FROM INTO @SCHOOL_SUBJECT, @SCHOOL_CRSE_NBR, @CRSE_ID, @CRSE_OFFER_NBR 

    WHILE @@FETCH_STATUS=0 
    BEGIN
        SELECT 'FROM: ' ,@SCHOOL_SUBJECT, @SCHOOL_CRSE_NBR, @CRSE_ID, @CRSE_OFFER_NBR 

        DECLARE DETAIL_CURSOR_TO CURSOR LOCAL FORWARD_ONLY READ_ONLY FOR 
        SELECT C.COMP_SUBJECT_AREA, C.CRSE_ID, C.CRSE_OFFER_NBR
        FROM PS_TRNSFR_COMP A 
        INNER JOIN PS_TRNSFR_TO C
            ON A.INSTITUTION=C.INSTITUTION
            AND A.TRNSFR_SRC_ID=C.TRNSFR_SRC_ID
            AND A.COMP_SUBJECT_AREA=C.COMP_SUBJECT_AREA
            AND A.TRNSFR_EQVLNCY_CMP=C.TRNSFR_EQVLNCY_CMP
            AND A.EFFDT=C.EFFDT
        WHERE A.INSTITUTION='THEINSTITUTION' AND A.TRNSFR_SRC_ID='0222' AND A.EFFDT='2019-06-21'
        AND A.TRNSFR_EQVLNCY_CMP=@SEQ
        AND A.COMP_SUBJECT_AREA=@SUBJECT

        OPEN DETAIL_CURSOR_TO
        FETCH NEXT FROM DETAIL_CURSOR_TO INTO @COMP_SUBJECT_AREA, @BEN_CRSE_ID, @BEN_OFFER_NBR         

        WHILE @@FETCH_STATUS=0 
        BEGIN
            SELECT 'TO: ', @COMP_SUBJECT_AREA, @BEN_CRSE_ID, @BEN_OFFER_NBR
            FETCH NEXT FROM DETAIL_CURSOR_TO INTO @COMP_SUBJECT_AREA, @BEN_CRSE_ID, @BEN_OFFER_NBR
        END

        CLOSE DETAIL_CURSOR_TO
        DEALLOCATE DETAIL_CURSOR_TO

        FETCH NEXT FROM DETAIL_CURSOR_FROM INTO @SCHOOL_SUBJECT, @SCHOOL_CRSE_NBR, @CRSE_ID, @CRSE_OFFER_NBR 
    END

    CLOSE DETAIL_CURSOR_FROM
    DEALLOCATE DETAIL_CURSOR_FROM

    FETCH NEXT FROM SUBJ_SEQ_CURSOR INTO @SUBJECT, @SEQ
END

CLOSE SUBJ_SEQ_CURSOR
DEALLOCATE SUBJ_SEQ_CURSOR

0 个答案:

没有答案