Transactions

  • Comments posted to this topic are about the item Transactions

  • Nice and easy. Thanks for sharing.

  • Great question, thanks.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Koen Verbeeck (10/9/2014)


    Great question, thanks.

  • There are no one completely correct answer.

    A1 are the most correct.

    If the answer hade been "The insert succeeds and the update fails BUT when are both rolled back".

    Have I right?

  • This was removed by the editor as SPAM

  • Stewart "Arturius" Campbell (10/9/2014)


    Good question, thanks Steve

    patrik.ljunggren (10/9/2014)


    There are no one completely correct answer.

    A1 are the most correct.

    If the answer hade been "The insert succeeds and the update fails BUT when are both rolled back".

    Have I right?

    No, there is no error handling (e.g. TRY / CATCH) and there is no rollback statement.

    Therefore, the transaction will begin, execute the INSERT, fail on the UPDATE, then commit.

    +1, that was really a good question, thanx Steve.

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • Good question, Steve. And the point it illustrates is an important one to understand.

  • From the information given, I'm missing why the update fails. We're told that it's a primary key, but that doesn't stop it being updated.

    create table test (id int NOT NULL PRIMARY KEY)

    GO

    INSERT test VALUES (1)

    GO

    UPDATE test SET id=2 WHERE id=1

    GO

    This works without error for me.

    I'd understand it failing if it were an IDENTITY but we weren't told that?

    What am I missing please folks?.

    Thanks for your indulgence.

    Keith

    Doh - forget it, I'd missed that there was already an item with PK of 3. Ignore this (but I'll leave it here in case it helps anyone else as dumb as me!).

  • The question assumes that XACT_ABORT is set to OFF. I know that's the default, but it would be worth mentioning in the question or explanation for clarity and completeness.

    John

  • Very good question, Steve.

    I learned this the hard way a looooong time ago while preparing some migration scripts.

    ---------------
    Mel. 😎

  • I assumed it was a trick so I chose incorrectly despite looking at the right answer.

    It proves that I didn't know the answer as confidently as I should have known.

  • Thank you for the post, Steve, very important one.

    TRY/CATCH first choice - or go classic (sql 2000 way, it works) store the error status in the variable on each DML statement and then use IF to check those variables not equal to zero then rollback and commit when both are zero.

    ww; Raghu
    --
    The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.

  • Ugh...I new I hadn't had enough coffee yet but answered anyway. I must have looked at it 3 times and never caught the missing Try...Catch.

    Aigle de Guerre!

  • Thanks for the question.

Viewing 15 posts - 1 through 15 (of 30 total)

You must be logged in to reply to this topic. Login to reply