If I kill a looping INSERT query, will it rollback all inserts since the loop started or only the last iteration?

dba.stackexchange https://dba.stackexchange.com/questions/262078

Pergunta

Do all inserts or only the current one rollback if the following loop query was killed in the middle of looping?

DECLARE @LoopId INT = 1;

WHILE (@LoopId < 100)
BEGIN
    INSERT INTO Table2
    SELECT Field1 
    FROM Table1
    WHERE ForeignKeyField = @LoopId

    SET @LoopId = @LoopId + 1;
END
Foi útil?

Solução

Do all inserts or only the current one rollback if the following loop query was killed in the middle of looping?

If the session has started a transaction before the loop, then all statements will be rolled back. But by default each statement runs in a separate transaction, and only the currently-running INSERT would be rolled back.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a dba.stackexchange
scroll top