Solved

NEWBIE: Transactionality through stored procedures?

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

747 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

13 Experts available now in Live!

Get 1:1 Help Now