mohamed
mohamed

Reputation: 1

I wrote this CURSOR to insert in table and it doesn't make any change

I wrote this CURSOR to insert into table and it doesn't make any change.

Please help

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

Upvotes: 0

Views: 45

Answers (1)

Alex
Alex

Reputation: 21766

You can achieve the same logic using SELECT, however, your cursor is likely not doing anything as the SELECT is empty.

 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

Upvotes: 2

Related Questions