MSBassSinger


When I run the following in Query Analyzer:

SET QUOTED_IDENTIFIER OFF -- allows double quotes (") used as wrapper for dynamic sql.
DECLARE @E int
BEGIN
TRAN T1
PRINT 'Start...'
EXEC("

begin tran T2|
raiserror( ' inside proc', 16, 11)
PRINT STR(@@ERROR)
rollback tran T2
PRINT STR(@@ERROR)
return

")

SET @E = @@ERROR
IF @E <> 0

BEGIN

PRINT 'Error ' + LTRIM(STR(@E)) + ' out of here...'

ROLLBACK TRAN T1

RETURN

END

PRINT 'Done.'

COMMIT TRAN T1

I get the following output and error:
Start...
Msg 50000, Level 16, State 11, Line 3
inside proc
50000
Msg 6401, Level 16, State 1, Line 5
Cannot roll back T2. No transaction or savepoint of that name was found.
6401
Msg 266, Level 16, State 2, Line 7
Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK TRANSACTION statement is missing. Previous count = 1, current count = 2.
Error 266 out of here...

Anyone have an idea why I would get the 6401 and 266 error

JD
Waleska, GA
MSBassSingerNO-SPAM@alltel.NO-SPAMnet
(just remove the string NO-SPAM)




Re: Transaction Question

Louis Davidson


SQL Server only physically supports a single transaction. Named transactions are only useful for marking the transaction log.

If you are trying to just roll back the stuff in the dynamic SQL, then you need to change the code to:

save tran T2
raiserror( ' inside proc', 16, 11)
PRINT STR(@@ERROR)
rollback tran T2
PRINT STR(@@ERROR)
return

Save tran lets you roll back part of a transaction

Also: Consider getting rid of the double quotes as that is not standard and not a good practice. Just double up quotes in the dynamic SQL:

'save tran T2
raiserror( '' inside proc'', 16, 11)
PRINT STR(@@ERROR)
rollback tran T2
PRINT STR(@@ERROR)
return'







Re: Transaction Question

MSBassSinger

Thanks. That did it.

We use the double quotes in our code so that the string that EXEC executes can have single quotes. It is neccessary for the type of scripts we write. Doubling up the single quotes does not work in the way we do it.

JD
Waleska, GA