MySQL Forums
Forum List  »  Stored Procedures

Re: updation order of fields in a table
Posted by: Rick James
Date: June 21, 2012 07:59PM

The SETs are probably done in order, thereby creating the "wrong" answer.

If you are doing just one row, this might be best:
BEGIN;   -- Assuming you are using InnoDB.
SELECT @adjustment := LEAST(AmtCr, AmtDr);
    FROM tbl1
    WHERE ...   -- assuming it hits exactly one row
    FOR UPDATE;
UPDATE tbl1 SET
        AmtDr = AmtDr - @adjustment,
        AmtCr = AmtCr + @adjustment
    WHERE ...;
COMMIT;
Note that I rewrote your confusing IFs into something more logical. (I am not sure I got the business logic correct.)

To do the entire table,
ALTER TABLE tbl1 ADD COLUMN `Adj` ...; -- done once
1. BEGIN
2. UPDATE SET Adj = ...
3. UPDATE SET AmtDr = ..., AmtCr = ...;
4. UPDATE SET Adj = 0;
5. COMMIT;
Keep this new column if you expect to repeat this process.

But why do it? You can always get the data you need without actually adjusting the values, can't you?

Options: ReplyQuote


Subject
Views
Written By
Posted
1397
June 20, 2012 10:26PM
Re: updation order of fields in a table
1018
June 21, 2012 07:59PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.