Solved

SQL Server TRANSACTION across mutliple SP calls

Posted on 2004-09-17
8
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

695 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