Solved

SQL Server TRANSACTION across mutliple SP calls

Posted on 2004-09-17
8
349 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

733 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