473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 8259
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.