Link to home
Start Free TrialLog in
Avatar of barnesd1
barnesd1

asked on

SQL Server TRANSACTION across mutliple SP calls

Hi there

I am working on a VB6 project that uses SQL Server 7 database.  I was hoping to retro-fit a transaction to cover all the processing for a each particular customer.  I was trying to issue a BEGIN TRANSACTION MyCustTran then do various processing and call 10 different stored procedures from VB.  It seems this can't be done?  Get error "Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK TRANSACTION statement is missing. Previous count = 1, current count = 0."
I was wondering if sp_getbindtoken could be used for this.  I'm trying to avoid major recoding of the application.  Hope you can help

Dave
Avatar of bangerarun
bangerarun

As your problem is no of BEGIN TRAN and COMMIT TRAN are mismatched  so
Dear There are various ways by whic you can do this

Insteaed of using the begin tran you should use the

BEGIN DISTRIBUTED TRAN

COMMIT TRAN

Also use the following block for ERROR trapping

after any statement (like insert/delete/update or any other)
if @@ERROR<>0
begin
  select @return="ERROR"     return can be defined as output parameter in procedure
   rollback tran
   return (99)
end


block..

As if you there is any error in any of consecutive procedure it will help you the control the transaction

But for this you should start the MSDTC service of SQL-Server which you can find in the service manager of SQL.

try with this you will definetely improve your proc performance

if any problem face send mail on bangerarun@yahoo.co.in
Avatar of barnesd1

ASKER

The question is where to put the
BEGIN DISTRIBUTED TRAN
statement.  I received the same error when I tried this in a stored proc on its own
Try this:

Sub TransactionDemo()

Dim Cn as new ADODB.Connection
Cn.ConnectionString = ....
...
Dim Cmd_1 as new ADODB.Command
...
Dim Cmd_n as new ADODB.Command
...
On error Goto ERRHandler
Cn.Open
Cn.BeginTrans

Set cmd.ActiveConnection = moConnection
cmd_1.CommandType = adCmdStoredProc
cmd_1.CommandText = "sp_1"
cmd_1.Execute
...
...
Set cmd.ActiveConnection = moConnection
cmd_n.CommandType = adCmdStoredProc
cmd_n.CommandText = "sp_N"
cmd_n.Execute
...
Cn..CommitTrans
cn.close

exit sub
ERRHandler:
Cn.RollbackTrans
Cn.Close
End Sub

Dear

begin distributed tran will be use in place of begin tran

and as you are saying that still the same error is coming then  please use

if @@ERROR<>0
begin
  select @return="ERROR1"     return can be defined as output parameter in procedure
   rollback tran
   return (99)
end

if @@ERROR<>0
begin
  select @return="ERROR2"     return can be defined as output parameter in procedure
   rollback tran
   return (99)
end

and so on after each transaction and check on which statement you are getting error

if possible send your code

Banger
I was trying to do a BEGIN TRAN in a sp on its own eg
Create Proc MyCustOpenTran as
Begin
Begin Transaction ThisCustomer
End

Create Proc MyCustCommitTran as
Begin
Commit Transaction ThisCustomer
End

Then in VB call

Exec MyCustOpenTran
Exec Sp1
Exec Sp2
Exec Sp3
Exec MyCustCommitTran

But I get the error about open transactions at the MyCustOpenTran stage
Do not use trans in SPs,

use them in VB,
start one

then do every SP call you want

the if every return code is OK
do a commit...
Please give example VB code for creating transactions - surely this is done on the db?
ASKER CERTIFIED SOLUTION
Avatar of Michael_D
Michael_D
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial