473,394 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

If Statement

Hi,

I am using the following if statment in my sql code. The problem i have is that the update statment runs successfully irregardless of wether my insert statment is successfull or not. What I need to do is if the insert is successfull run the update statement and if the insert fails at any point the update statment should fail.

The SQL statment is as follows:

IF EXISTS (select from table1statment stating the condition to excute the insert statment)
INSERT INTO table1(a,b)
select (a,b) from table2
UPDATE table1
SET flag = 'DONE' WHERE condition.


Thanks.
Jul 2 '07 #1
6 2683
srinit
43
Hi,

I am using the following if statment in my sql code. The problem i have is that the update statment runs successfully irregardless of wether my insert statment is successfull or not. What I need to do is if the insert is successfull run the update statement and if the insert fails at any point the update statment should fail.

The SQL statment is as follows:

IF EXISTS (select from table1statment stating the condition to excute the insert statment)
INSERT INTO table1(a,b)
select (a,b) from table2
UPDATE table1
SET flag = 'DONE' WHERE condition.


Thanks.

Hi
Use transactions
by putting the insert and update statements in a transaction you can get this.
Jul 2 '07 #2
Hi
Use transactions
by putting the insert and update statements in a transaction you can get this.
Thanks for the help

How can I get the transaction to fail so that I know it works by putting it into transaction.

Thanks
Jul 2 '07 #3
It Might Be Just As Easy To Check The @@Error And @@ROWCOUNT System Variables After The INSERT Statement.

Eg

If EXISTS (....)
BEGIN
INSERT...

Check If No Error & Row Successfully Inserted.
IF (@@Error == 0) AND (@@ROWCOUNT <> 0)
BEGIN
UPDATE....
END
END
Jul 2 '07 #4
ck9663
2,878 Expert 2GB
Hi,

I am using the following if statment in my sql code. The problem i have is that the update statment runs successfully irregardless of wether my insert statment is successfull or not. What I need to do is if the insert is successfull run the update statement and if the insert fails at any point the update statment should fail.

The SQL statment is as follows:

IF EXISTS (select from table1statment stating the condition to excute the insert statment)
INSERT INTO table1(a,b)
select (a,b) from table2
UPDATE table1
SET flag = 'DONE' WHERE condition.


Thanks.
try:
IF EXISTS (select...)
BEGIN
begin transaction
INSERT INTO table1(a,b)
select (a,b) from table2
UPDATE table1
SET flag = 'DONE' WHERE condition.

---Check If No Error & Row Successfully Inserted.
IF (@@Error == 0) AND
commit
else
BEGIN
rollback transaction
--put other error handling scripts here
END

END

consider this:
1. @@error checks if the previous statement runs successfully. in this case the last statement is UPDATE, not INSERT. you can check the @@error after the insert and rollback or commit depending the result. then do the UPDATE, then check it again.
Jul 2 '07 #5
There Seems To Be A Number Of Issues Here.

There Are 5 Steps

If EXISTS
BEGIN
INSERT
SELECT
UPDATE
SET
END

From What You Said, You Only Want To Continue To The Next Step If The Previous Step Was Successful.

That's Fine, But You May Need To Consider Why It Might Fail & Do Something About It.

Also, If Say The UPDATE Fails, Do You Want To RollBack The INSERT ???

If So, Then You Will Need To Use A Transaction, As My Learned Colleague Suggested. You'll Need To Check For Success/Failure After Each Of The INSERT/SELECT/UPDATE Statements And Use A GOTO Statement To Jump To The End Of The Procedure If There Is An Error.
Jul 2 '07 #6
try:
IF EXISTS (select...)
BEGIN
begin transaction
INSERT INTO table1(a,b)
select (a,b) from table2
UPDATE table1
SET flag = 'DONE' WHERE condition.

---Check If No Error & Row Successfully Inserted.
IF (@@Error == 0) AND
commit
else
BEGIN
rollback transaction
--put other error handling scripts here
END

END

consider this:
1. @@error checks if the previous statement runs successfully. in this case the last statement is UPDATE, not INSERT. you can check the @@error after the insert and rollback or commit depending the result. then do the UPDATE, then check it again.


Thanks a lot mate. I will try this later and let you know the results. sounds ok.
Jul 3 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: Fábio Mendes | last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with things or the python-list sent my msg to /dev/null. I couldn't find anything related in previous PEP's, so here it goes a...
15
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
13
by: eman1000 | last post by:
I was recently looking at the prototype library (http://prototype.conio.net/) and I noticed the author used the following syntax: Object.extend(MyObj.prototype, { my_meth1: function(){},...
37
by: Steven Bethard | last post by:
The PEP below should be mostly self explanatory. I'll try to keep the most updated versions available at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
18
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
28
by: Steven Bethard | last post by:
Ok, I finally have a PEP number. Here's the most updated version of the "make" statement PEP. I'll be posting it shortly to python-dev. Thanks again for the previous discussion and suggestions!...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
19
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without...
18
by: dspfun | last post by:
Hi! The words "expression" and "statement" are often used in C99 and C- textbooks, however, I am not sure of the clear defintion of these words with respect to C. Can somebody provide a sharp...
23
by: florian.loitsch | last post by:
According to the spec Section 14 the production SourceElements:SourceElements SourceElement is evaluated as follows: 1. Evaluate SourceElements. 2. If Result(1) is an abrupt completion, return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
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...
0
jinu1996
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.