Solved

SQL Server TRANSACTION across mutliple SP calls

Posted on 2004-09-17
8
338 Views
Last Modified: 2013-12-25
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
0
Comment
Question by:barnesd1
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 1

Expert Comment

by:bangerarun
ID: 12100971
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
0
 
LVL 4

Author Comment

by:barnesd1
ID: 12101063
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
0
 
LVL 13

Expert Comment

by:Michael_D
ID: 12138470
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

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Expert Comment

by:bangerarun
ID: 12157915
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
0
 
LVL 4

Author Comment

by:barnesd1
ID: 12159022
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
0
 
LVL 2

Expert Comment

by:YSkelling
ID: 12353722
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...
0
 
LVL 4

Author Comment

by:barnesd1
ID: 12355523
Please give example VB code for creating transactions - surely this is done on the db?
0
 
LVL 13

Accepted Solution

by:
Michael_D earned 500 total points
ID: 12356784
I have posted vb code long time ago, just try it :)
Maybe you want to check return value of sp as well  like 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_1.ActiveConnection = moConnection
cmd_1.CommandType = adCmdStoredProc
cmd_1.CommandText = "sp_1"
cmd_1.Execute

If cmd_1.parameters("@Return_Value")<>0 then GOTO ERRHandler
...
...
Set cmd_n.ActiveConnection = moConnection
cmd_n.CommandType = adCmdStoredProc
cmd_n.CommandText = "sp_N"
cmd_n.Execute

If cmd_n.parameters("@Return_Value")<>0 then GOTO ERRHandler
...
Cn..CommitTrans
cn.close

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question