Solved

SQL Server TRANSACTION across mutliple SP calls

Posted on 2004-09-17
8
317 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Expert Comment

by:bangerarun
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 4

Author Comment

by:barnesd1
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now