Solved

NEWBIE: Transactionality through stored procedures?

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql 2014,  lock limit 5 32
2016 SQL Licensing 7 41
Correct an issue with a where clause with calculation 2 31
SQL Server Import/Error Wizard error 12 19
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

810 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