• Richard Sisk (10/5/2009)


    Huh, what?! I got the following when I tested my answer...

    (1 row(s) affected)

    Msg 208, Level 16, State 1, Line 2

    Invalid object name 'T'.

    Where did I go wrong in my life...

    I got the same thing when I ran this code:

    CREATE TABLE T

    ( ID int NOT NULL,

    Code varchar(10) NULL)

    INSERT INTO T SELECT 1,'AAAA' GO

    BEGIN Tran t1

    DROP TABLE T

    ROLLBACK Tran GO

    SELECT * FROM T

    But when I moved the "GO" in the ROLLBACK Tran GO to the next line and ran it as this:

    CREATE TABLE T

    ( ID int NOT NULL,

    Code varchar(10) NULL)

    INSERT INTO T SELECT 1,'AAAA' GO

    BEGIN Tran t1

    DROP TABLE T

    ROLLBACK Tran

    GO

    SELECT * FROM T

    it ran just fine with the expected response. SQL was probably looking at the GO as the alias for Tran and that was messing things up as there was no GO transaction. At least, that's what I think is the reason behind why it didn't work the first time.

    -- Kit