Hi Everyone,
In brief, I am trying to get the whole block of code done in one go or not to do at all.
Here is a piece of code I am trying:
[sql]
BEGIN TRANSACTION
INSERT INTO authors (au_id, au_lname, au_fname, phone, contract)
VALUES ('172-32-1176', 'Mickey', 'Mouse', '222-33-4444', 1)
IF @@ERROR <>0
BEGIN
ROLLBACK TRAN
PRINT 'STEP 1 DONE'
print @@ERROR
END
UPDATE authors
SET au_fname = 'pickey'
WHERE au_id = '172-32-1176'
IF @@ERROR <>0
BEGIN
ROLLBACK TRAN
PRINT 'STEP 2 DONE'
print @@ERROR
END
COMMIT TRANSACTION
[/sql]
Though it shows primary key constraint and not doing the insert, it still moves to update.
I want it to insert and than update or not do anything at all - means if it can not insert, than end rather than do update.
Please help. I am sure I am missing something very obvious but can not figure out what.
Thanks a lot for your help in advance.
Sree
1 1699 ck9663 2,878
Recognized Expert Specialist
Hi Everyone,
In brief, I am trying to get the whole block of code done in one go or not to do at all.
Here is a piece of code I am trying:
[sql]
BEGIN TRANSACTION
INSERT INTO authors (au_id, au_lname, au_fname, phone, contract)
VALUES ('172-32-1176', 'Mickey', 'Mouse', '222-33-4444', 1)
IF @@ERROR <>0
BEGIN
ROLLBACK TRAN
PRINT 'STEP 1 DONE'
print @@ERROR
END
UPDATE authors
SET au_fname = 'pickey'
WHERE au_id = '172-32-1176'
IF @@ERROR <>0
BEGIN
ROLLBACK TRAN
PRINT 'STEP 2 DONE'
print @@ERROR
END
COMMIT TRANSACTION
[/sql]
Though it shows primary key constraint and not doing the insert, it still moves to update.
I want it to insert and than update or not do anything at all - means if it can not insert, than end rather than do update.
Please help. I am sure I am missing something very obvious but can not figure out what.
Thanks a lot for your help in advance.
Sree
i think @@error will reset after each successful statement so you have to get the value before you execute a next statement. i believe a trigger will be best use in thise case
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: noah |
last post by:
Does PHP have a feature to associate Cookie sessions with a persistent
database connection that will allow a single transaction across
multiple HTTP requests?
Here is how I imagine my process: I have an series of interactive HTML
forms. The user begins a Cookie session. A database connection is
opened and a transaction is begun. After the user goes through any
number of pages where they update the database they finish on a page
where...
|
by: Alan |
last post by:
Gidday people,
I had a bit of a problem this morning. I think I've got it sorted now, but I
wonder if anyone can shed some light.
I have a plain VBS WHS file that instantiates a component that controls an
interface file import process. This component, Controller, is part of
FinInterface.dll, and instantiates another component, Implementor (same
dll), that does the actual data access (an insert into a FinanceDataImports
table, and then...
|
by: Steve Jorgensen |
last post by:
Hi all,
I've migrated an Access database schema to PostgreSQL, and I'm trying to get
everything working again. I'm having the following problem...
Within an Access transaction, I insert a row into one table, then I insert a
row into a table with a foreign key relationship to the first record. The
second insert fails with a referential integrity violation referring to the
same relationhip I mentioned. This worked fine with a JET...
|
by: REB |
last post by:
If I create a transaction object for rolling back a insert query in the on
click event of a form button can the transaction be tracked across other
functions?
For example:
private void btnSaveDriver_Click(object sender, System.EventArgs e)
{
|
by: jacob.miles |
last post by:
Hello. I'm trying to wrap a function call in a transaction, but when I
intentionally throw an exception in the middle of the function it
doesn't actually roll back the transaction. The debug output says
1/ROLLBACK, without any 1/COMMITs in there, but when I view the data in
the command-line mysql utility the changes have been made.
This is the code I'm using to connect to the mysql database and to wrap
the function call in a...
| |
by: cricketweb |
last post by:
I have a stored procedure that calls another stored procedure with the
first stored procedure opening a transaction:
BEGIN
SET XACT_ABORT ON
BEGIN TRANSACTION
does various updates/inserts
|
by: Allan Ebdrup |
last post by:
I'm using dotNet 2.0 and System.Transactions to run transactions that span
multiple database queries, Now I would like to log any Sql errors that occur
in the transaction, but when I insert the logentry into the database the
query that does the inserting is automatically enlisted in the running
transaction and rolled back when the transaction aborts.
How do I run the query inserting into the log outside the current
transaction, so that it...
|
by: qhjghz |
last post by:
Hi All,
The transaction isolation level for my stored procedure is READ COMMITTED. As far as I read, in this isolation level, "In effect, a SELECT query sees a snapshot of the database as of the instant that that query begins to run." But, I observed a deviation from this. In my session, I am explicitly starting a transaction and inserting a row into the Property table (say). This property belongs to the City Maine(say). However, I am...
|
by: Gordon |
last post by:
I am currently working on some code for my CMS that creates a site
folder, then creates all the necessary child folders inside it. The
method that creates folders needs to insert into 2 tables so it wraps
the operation in a transaction. (psuedocode below)
public function createItem ($parent)
{
$this -database -beginTransaction ();
$this -database -query ('INSERT INTO items (param1,
param2 ... ) values (val1, val2 ... )');
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |