Può un'istruzione SQL "Elimina" seguita da un'istruzione "Whow Not in" essere nella stessa transazione?

StackOverflow https://stackoverflow.com/questions/5858685

Domanda

Se ho il seguente blocco SQL (in SQL Server 2008 R2):

BEGIN
    BEGIN TRAN

    DELETE FROM dbo.fooData
    WHERE LastUpdate < DateAdd(hour, -1,GETUTCDATE())

    COMMIT

    BEGIN TRAN

    DELETE FROM dbo.barData
    WHERE SessionID NOT IN (SELECT sub.SessionId FROM dbo.fooData sub)

    COMMIT
    RETURN 0
END

Suppongo che devo fare un commit esplicito tra le dichiarazioni affinché i dati eliminati da Foodata vengano visualizzati nella seconda eliminazione. È corretto? Idealmente, vorrei che tutto questo fosse in una transazione. Esempio:

BEGIN
    BEGIN TRAN

    DELETE FROM dbo.fooData
    WHERE LastUpdate < DateAdd(hour, -1,GETUTCDATE())

    DELETE FROM dbo.barData
    WHERE SessionID NOT IN (SELECT sub.SessionId FROM dbo.fooData sub)

    COMMIT
    RETURN 0
END

La mia paura è che la seconda affermazione non raccoglierà i dati eliminati del primo. Nota, il ritorno è lì perché fa parte di una procedura memorizzata. Non sono interessato a eliminare a cascata le eliminazioni o aderire, sono in qualche modo vincolato a questo metodo.

È stato utile?

Soluzione

The same transaction/session can see it's own changes. Other sessions won't see these uncommitted transactions from this session

So your 2nd form (one wider transaction) is safe to use.

Altri suggerimenti

You can use the second example.

Because you're in the same transaction, you are accessing to the information of the previous operations.

Isolation says that other operations cannot access data that has been modified during a transaction that has not yet completed. The question of isolation occurs in case of concurrent transactions (multiple transactions occurring at the same time). but you are int the same transaction, so that transaction can and will access to the result of first operation.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top