我写了这个CURSOR插入表格,它没有做任何改变

时间:2016-02-17 10:29:42

标签: sql-server database

我写了这个CURSOR插入到表中,它没有做任何改变。

请帮忙

DECLARE @ycard   varchar
DECLARE @Indate  datetime
DECLARE @outdate datetime
DECLARE @outtime datetime

DECLARE @MyCursor CURSOR
SET @MyCursor = CURSOR FAST_FORWARD FOR
    SELECT ycard, indate, outdate, outtime
    FROM EMPTRANS07
    WHERE ycard = 1520015

OPEN @MyCursor
FETCH NEXT FROM @MyCursor INTO @ycard, @Indate, @outdate, @outtime

WHILE @@FETCH_STATUS = 0
BEGIN
    IF @Indate > @outdate --AND @outtime > '1899-12-30 08:30:00.000' 
    BEGIN       
        INSERT INTO ATTRANS07 ([Card_ID], [date_att], [time_att], [Transaction_type])
        VALUES (@ycard,(@Indate + 1) ,'1899-12-30 08:30:00.000',1);
     END

     FETCH NEXT FROM @MyCursor INTO @ycard, @Indate, @outdate, @outtime
END

CLOSE @MyCursor
DEALLOCATE @MyCursor

1 个答案:

答案 0 :(得分:2)

您可以使用SELECT实现相同的逻辑,但是,由于SELECT为空,您的光标可能无法执行任何操作。

 INSERT INTO ATTRANS07
       ( [Card_ID] ,
        [date_att] ,
        [time_att] ,
        [Transaction_type]
       )
       SELECT ycard ,
             indate + 1 ,
             '1899-12-30 08:30:00.000' ,
             1
       FROM  EMPTRANS07
       WHERE     ycard = 1520015
       AND indate > outdate