@@ FETCH_STATUS<> -1 AND @@ FETCH_STATUS<> -2是什么意思?

时间:2017-09-07 18:31:47

标签: sql-server sql-function

我遇到了一个考试问题,其中包含以下代码:

ALTER FUNCTION fncF2 (@HWStreamIndex int = 0)
RETURNS nvarchar(max)
AS
BEGIN

    DECLARE @OnSiteGroups AS nvarchar(150)
    DECLARE @OnSiteGroupDetails AS nvarchar(1000)
    DECLARE @HWOnSiteGroupIndex AS int
    DECLARE @OnSiteGroupsTotal AS nvarchar(max)
    SET @OnSiteGroupsTotal = ''

    DECLARE cursor1 CURSOR
    READ_ONLY FOR
            SELECT
                HWOnSiteGroupName + '-' AS OnSiteGroups,
                HWOnSiteGroupIndex
            FROM 
                HW_STREAM_ONSITE_GROUP
            WHERE 
                HWStreamIndex = @HWStreamIndex

    OPEN cursor1

    FETCH NEXT FROM cursor1 INTO @OnSiteGroups, @HWOnSiteGroupIndex
    WHILE (@@fetch_status <> -1)
    BEGIN
        IF (@@fetch_status <> -2)
        BEGIN
            BEGIN
                SET @OnSiteGroupsTotal = @OnSiteGroupsTotal + @OnSiteGroups + ' -- '

                DECLARE cursor2 CURSOR
                READ_ONLY FOR
                        SELECT
                            ISNULL(HW_MANAGE_METH_CODES.HWMMCode, '') + '_' +
                            CONVERT(nvarchar(20), CONVERT(decimal(12, 2), 
                            ISNULL(HW_STREAM_ONSITE_GROUP_DETAILS.PercentProcessed, 0.0) * 100)) + '%' AS OnSiteGroups
                        FROM 
                            HW_STREAM_ONSITE_GROUP_DETAILS
                            JOIN HW_STREAM_ONSITE_GROUP
                                ON HW_STREAM_ONSITE_GROUP_DETAILS.HWOnSiteGroupIndex = HW_STREAM_ONSITE_GROUP_DETAILS.HWOnSiteGroupIndex
                            LEFT JOIN HW_MANAGE_METH_CODES
                                ON HW_STREAM_ONSITE_GROUP_DETAILS.HWMMCodeIndex = HW_MANAGE_METH_CODES.HWMMCodeIndex
                        WHERE 
                            HW_STREAM_ONSITE_GROUP_DETAILS.HWOnSiteGroupIndex = @HWOnSiteGroupIndex

                OPEN cursor2

                FETCH NEXT FROM cursor2 INTO @OnSiteGroupDetails
                WHILE (@@fetch_status <> -1)
                BEGIN
                    IF (@@fetch_status <> -2)
                    BEGIN
                        BEGIN
                            SET @OnSiteGroupsTotal = @OnSiteGroupsTotal + @OnSiteGroupDetails + ', '
                        END
                    END
                    FETCH NEXT FROM cursor2 INTO @OnSiteGroupDetails
                END

                CLOSE cursor2
                DEALLOCATE cursor2

            END
        END
        FETCH NEXT FROM cursor1 INTO @OnSiteGroups, @HWOnSiteGroupIndex
    END

    CLOSE cursor1
    DEALLOCATE cursor1

    RETURN @OnSiteGroupsTotal
END

通常我在使用游标时使用@@FETCH_STATUS = 0。我试图理解这段代码是如何工作的,以及@@FETCH_STATUS <> -1@@FETCH_STATUS <> -2如何影响整体执行,但我无法掌握这段代码。

有人可以解释一下语句@@FETCH_STATUS <> -1@@FETCH_STATUS <> -2的目的是什么,这个函数的执行步骤是什么?

1 个答案:

答案 0 :(得分:1)

正如Microsoft Documentation中所述,有4个可能的@@FETCH_STATUS值,每个值代表不同的状态消息:

  • 0 - FETCH声明成功。
  • -1 - FETCH语句失败或行超出结果集。
  • -2 - 提取的行已丢失。
  • -9 - 游标未执行提取操作。

WHILE (@@fetch_status <> -1)告诉循环运行只要 最后一个游标FETCH语句成功,行在结果集中。

IF (@@fetch_status <> -2)正在检查以确保该行存在,然后再继续执行该过程。