Solved

NEWBIE: Transactionality through stored procedures?

Posted on 2007-03-26
5
231 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

948 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

18 Experts available now in Live!

Get 1:1 Help Now