Solved

NEWBIE: Transactionality through stored procedures?

Posted on 2007-03-26
5
252 Views
Last Modified: 2012-06-21
Dear Experts,

I may be misusing the term "transactionality", so here's the simple explanation:

I want to write a stored procedure that does this:
INSERT INTO TABLE_A VALUES "A"
INSERT INTO TABLE_B VALUES "B"

And if either fails to insert, BOTH should fail to insert.  

First, Is the use of a stored procedure a valid way to accomplish this?  

And second, will it still work if the database's recovery mode is set to simple? If not, will anything work if recovery mode is set to simple?

Thanks!
BrianMc1958




0
Comment
Question by:BrianMc1958
5 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 200 total points
ID: 18795796
create procedure testSp
as
BEGIN TRAN
 INSERT INTO TableA SELECT ..
 IF @@ERROR <> 0 OR @@ROWCOUNT = 0
 BEGIN
   ROLLBACK TRAN
   RETURN
 END
 INSERT INTO TableB SELECT ..
 IF @@ERROR <> 0 OR @@ROWCOUNT = 0
 BEGIN
   ROLLBACK TRAN
   RETURN
 END


COMMIT TRAN
0
 
LVL 16

Assisted Solution

by:rboyd56
rboyd56 earned 150 total points
ID: 18795862
<And second, will it still work if the database's recovery mode is set to simple? If not, will anything work if recovery mode is set to simple?>

Yes this will work if the recovery model is simple. The recovery model does not affect the types of transactions that can be used. It determines the database recoverability options.
0
 

Author Comment

by:BrianMc1958
ID: 18795866
Yikes.  I'm a newbie, and I vaguely thought that just using a stored procedure would guarantee an all-or-nothing result.  You're including explicit ROLLBACKs, which would be very complex to do in our real-world problem.  

Also, I thought ROLLBACKs were ignored if recovery mode was simple?
0
 

Author Comment

by:BrianMc1958
ID: 18795874
My comment and rboyd56's comment crossed in the mail...
0
 
LVL 23

Assisted Solution

by:Racim BOUDJAKDJI
Racim BOUDJAKDJI earned 150 total points
ID: 18798759
Your options are the following:

--> Manage the second insert through a stored proc
--> set up a trigger on INSERT on the first the table to automatically insert a corresponding record in the second table
--> avoid dupplication at all.  Unless imperative, data should be stored only store once in a db.  Chances are you could improve your schema by eliminating redundancy and normalization.

If you can's do normalization then go with the stored proc solution...
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

821 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