Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi SQL Gurus,

In my stored procedure, I am using transaction and also calling other stored procedure. Some pseudo-code

SQL
begin transaction
    Update TableA set colA = 'Updated'
    exec SomeotherStoredProc
    if @@Error=0
        Commit Transaction
    else
        RollbackTransaction

In Other stored procedure, I am checking if TableA.ColA has value 'Updated' or not. But I am not getting the value as 'Updated'.

I am aware and tried and confirmed that if I am in same stored procedure, I am getting updated value in TableA.ColA.

Is this correct behavior? If yes, I would like to read further. Please guide.
If No, What am I missing?
Thanks and regards,
Milind

What I have tried:

Tried debugging and found updated value NOT available in child procedure but is available in same stored procedure.
Posted
Updated 5-Sep-16 7:20am
v2
Comments
Wendelius 5-Sep-16 12:30pm    
What is the code for SomeotherStoredProc?
MT_ 5-Sep-16 12:54pm    
it is just accessing table A "select colA from TableA" and checking if value is 'Updated' for further processing..

1 solution

If your inside the same transaction then you should see the updated value.

Try the following test
SQL
CREATE TABLE TableA (
   ColA nvarchar(100)
);

INSERT INTO TableA VALUES ('Empty');

CREATE PROCEDURE ProcB AS
DECLARE
   @currentvalue nvarchar(100);
BEGIN
   SELECT @currentvalue = ColA FROM TableA;
   PRINT 'ProcB: ' + @currentvalue + ', trancount: ' + CAST(@@trancount AS nvarchar(10));
END;

CREATE PROCEDURE ProcA AS
DECLARE
   @currentvalue nvarchar(100);
BEGIN
   BEGIN TRANSACTION;
   UPDATE TableA SET colA = 'Updated';
   EXEC ProcB
   ROLLBACK;
   SELECT @currentvalue = ColA FROM TableA;
   PRINT 'ProcA: ' + @currentvalue + ', trancount: ' + CAST(@@trancount AS nvarchar(10));
END;

EXEC ProcA

The result should be
(1 row(s) affected)
ProcB: Updated, trancount: 1
ProcA: Empty, trancount: 0

So based on this the problem lies elsewhere. Check for example:
- Are you really inside the same transaction, for example no linked server involved
- Did the UPDATE statement actually update any rows, is the modification done
- If WHERE clause is involved, do you have the same conditions etc...

Where applicable print the intermediate results and/or conditions to ensure that the code path is executed as expected.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900