Question

I want to use a cursor for inserting data.

ALTER PROCEDURE [dbo].[VehBlocMajStatut] 
-- Add the parameters for the stored procedure here
@NO_Veh int,
@Statut int

AS
BEGIN

DECLARE @Temp_appel int
DECLARE appel_cursor CURSOR FOR 
SELECT NO_APPEL FROM ESPMEDS_LS_APPVEH WHERE NO_VEHICULE = @NO_Veh

OPEN appel_cursor;

FETCH NEXT FROM appel_cursor INTO @Temp_appel;

WHILE @@FETCH_STATUS = 0
BEGIN
    INSERT INTO ESPMEDS_LS_APPVEH (NO_VEHICULE, NO_APPEL, STATUT, DATEVEH)
    VALUES (@NO_Veh, @Temp_appel, @Statut, GETDATE())

    FETCH NEXT FROM appel_cursor INTO @Temp_appel;


END
close appel_cursor
deallocate appel_cursor
END

Unfortunately it turns into an infinite loop. When I commented the INSERT INTO clause then it works normally.

How can I do an insert with a cursor?

Was it helpful?

Solution

No cursor is needed.

INSERT INTO ESPMEDS_LS_APPVEH 
    (NO_VEHICULE, NO_APPEL, STATUT, DATEVEH)
    SELECT @NO_Veh, NO_APPEL, @Statut, GETDATE()
        FROM ESPMEDS_LS_APPVEH 
        WHERE NO_VEHICULE = @NO_Veh
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top