1
DECLARE  AssigChart CURSOR FOR SELECT  vad_id_no, drv_id_no, vad_st_dt, vad_st_tm, vad_fn_dt, vad_fn_tm FROM lu02vad WHERE lTachoSmartCardID = @tachoSmartCardID

    OPEN AssigChart FETCH NEXT FROM AssigChart INTO @ChartID, @drvid, @dStartTime, @lStartTime, @dEndTime, @lEndTime
    WHILE (@@FETCH_STATUS <> -1)
    BEGIN


<------------------some processing ---------------------->>>>>>>>


FETCH NEXT FROM AssigChart 
         INTO @ChartID, @drvid, @dStartTime, @lStartTime, @dEndTime, @lEndTime
    END
    CLOSE AssigChart
    DEALLOCATE AssigChart
4

2 回答 2

1

@@FETCH_Status 返回以下值....

0 The FETCH statement was successful.
-1 The FETCH statement failed or the row was beyond the result set.
-2 The row fetched is missing.

所以如果不成功,它可以返回 -1 和 -2 任何人,所以你可以像这样检查它....

while @@FETCH_STATUS=0

或者

while @@FETCH_STATUS<>-1 and @@FETCH_STATUS<>-2
于 2013-06-04T06:46:11.750 回答
1

试试这个——

DECLARE AssigChart CURSOR READ_ONLY LOCAL FAST_FORWARD FOR 
    SELECT vad_id_no, drv_id_no, vad_st_dt, vad_st_tm, vad_fn_dt, vad_fn_tm 
    FROM lu02vad 
    WHERE lTachoSmartCardID = @tachoSmartCardID

OPEN AssigChart 

FETCH NEXT FROM AssigChart INTO           
          @ChartID
        , @drvid
        , @dStartTime
        , @lStartTime
        , @dEndTime
        , @lEndTime

WHILE @@FETCH_STATUS = 0 BEGIN

    -- your statements

    FETCH NEXT FROM AssigChart INTO 
          @ChartID
        , @drvid
        , @dStartTime
        , @lStartTime
        , @dEndTime
        , @lEndTime

END

CLOSE AssigChart
DEALLOCATE AssigChart
于 2013-06-04T07:27:25.987 回答