472,989 Members | 3,105 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

transaction inside sp_executesql

Hi to all,

Probably I'm just doing something stupid, but I would like you to tell
me that (if it is so), and point the solution.

There ist the thing:

I' having a sp, where I call other sp inside.
The only problem is, the name of this inside sp is builded variously,
and executed over sp_executesql:

create pprocedure major_sp
@prm_outer_1 varchar(1),
@prm_outer_2 varchar(2)
as

some coding

set @nvar_stmtStr = N'exec @int_exRetCode = test_sp_' + @prm_outer_1 +
@prm_outer_2
set @nvar_stmtStr = @nvar_stmtStr + ' @prm_1, @prm_2, @prm_3, @prm_4
output'

set @nvar_prmStr = N'@prm_1 nvarchar(128), ' +
N'@prm_2 nvarchar(128), ' +
N'@prm_3 nvarchar(4000), ' +
N'@int_exRetCode int output, ' +
N'@prm_4 varchar(64) output'

exec sp_executesql @nvar_stmtStr,
@nvar_prmStr,
@prm_1,
@prm_2,
@prm_3,
@int_exRetCode = @int_exRetCode output,
@prm_4 = @prm_4 output
Now the issue is, I've transactions inside test_sp_11 lets say where
the 11 is @prm_outer_1 + @prm_outer_2.
These procedures are existing inside database, but are called dynamicly
depending of the parameters.

The problem is, when I call the specified sp directly, the rollback
transaction is working without any problem.
Inside this procedures test_sp_xx, is a call of another sp (lets say
inside_sp).
There is a transaction included.
When it is called over major_sp, then the rollback is not performed
because of error:

Server: Msg 6401, Level 16, State 1, Procedure inside_sp, Line 54
Cannot roll back transactio_bubu. No transaction or savepoint of that
name was found.

The funniest way is, if there is no error inside, the commit is working
without any problem!

The question is majory (because I'm almost sure, that this is an
issue): is it possible, to have a
transaction inside dynamicly called sp over sp_executesql?
If ok to do that?

Thank's in advance

Matik

Sep 7 '06 #1
1 8200
Matik (ma****@sauron.xo.pl) writes:
I' having a sp, where I call other sp inside.
The only problem is, the name of this inside sp is builded variously,
and executed over sp_executesql:
There is no need for that. Simply do:

SELECT @spname = test_sp_' + @prm_outer_1 + @prm_outer_2
EXEC @ret = @spname @prm_1, @prm_2, @prm_3, @prm_4 OUTPUT
The problem is, when I call the specified sp directly, the rollback
transaction is working without any problem.
Inside this procedures test_sp_xx, is a call of another sp (lets say
inside_sp).
There is a transaction included.
When it is called over major_sp, then the rollback is not performed
because of error:

Server: Msg 6401, Level 16, State 1, Procedure inside_sp, Line 54
Cannot roll back transactio_bubu. No transaction or savepoint of that
name was found.

The funniest way is, if there is no error inside, the commit is working
without any problem!
This nothing to do with dynamic SQL at all. It has to do with named
transaction, a feature that I have will have to admit never really
found the point with.

Here is a repro that shows what is going on:

begin transaction "outer"
begin transaction "inner"
rollback transaction "inner"
rollback transaction

This yields the same error message as you got. But if you only run the
two lines in the middle, then it works as advertised.

The key issue is here when you nest BEGIN TRANSACTION, the inner of
them only increases the transaction counter. When you commit a nested
transaction, you only decrease that counter if the counter is 1.

But a ROLLBACK is different. A ROLLBACK always rolls back it all. Here
you ask to rollback to "inner" but you can't, because there is no
transaction with that name.

If you instead change the inner BEGIN TRANSACTION to SAVE TRANSACTION,
the rollback will succeed. You have then defined a savepoint to which
a transaction can be rolled back and then continue from that point.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Sep 7 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: LineVoltageHalogen | last post by:
Greetings All, currentley there is a heated discussion in my place of work over which method is better/more efficient for simple selects. Background: 1.) Simple Application that uses sql server...
8
by: Alex | last post by:
Hi, I have a test system that is setup the same as a production system and would like to frequently copy the database over. pg_dump takes a few hours and even sometimes hangs. Are there any...
9
by: Chance Hopkins | last post by:
When using the sqlclient dotnet class and sql server. is it faster when doing a single (not multiple) insert to: 1- just do a simple connection and command object, then execute a nonquery ...
2
by: Dan Kelley | last post by:
I have 2 services, ServiceA and ServiceB. Certain user driven functions require ServiceA to perform some DB tasks, before sending a request to ServiceB to perform some additional tasks. If ServiceB...
1
by: Mana | last post by:
Hi, I want to implement nested transactions in C#. When I write BEGIN TRANSACTION inside another BEGIN TRANSACTION in an SQL Script it works fine. But when I call BeginTransaction() inside...
1
by: satishchandrat | last post by:
Hi, This is regarding the sp_executesql and the sql statement parameter, in processing a dynamic SQL on SQL Server 2000, in my stored procedure. I have my SQL string exeeding more than 4000...
2
by: Ryan Liu | last post by:
Hi, I have few db write and read to execute, so I use transaction. Is that a problem or is that a regular way that I only use transaction on some cmds only, and other cmds I do not use...
0
by: Matik | last post by:
Hi, MSSQL 2000 booth servers. Booth running DTC. Now, the client application, is starting in DB1 a procedure. The connection open to db, is within the transation opened from client. In the...
5
by: Yash | last post by:
Hi, I am using SQL 2000 SP4. I have compared 2 scenarios: Scenario 1: insert into #bacs_report SELECT ..... WHERE <conditions>
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.