Solved

SQL Update table

Posted on 2002-03-30
3
172 Views
Last Modified: 2010-04-04
Hi

I'm using Dbisam components and have a question.

I have 3 fields (integer) named aField, bField, cField.
it contains numbers like
0  , 20 , 20
20 , 25 , 45
45 , 10 , 55

cField is sum of aField and bField, and aField is sum of cField in previous record.

If i will change the bField  in record1 from 20 to 25 how can i write a SQL command so the whole table will update to

0  , 25 , 25
25 , 25 , 50
50 , 10 , 60

My SQL knowledge is very small.
I have solved it until now with scrolling through the table with for i=1 to table1.recordcount and so on but its not a good solution , specially when the table will contains a lot more records.
Hope you understand my question.
0
Comment
Question by:InterDive
3 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6908551
? in sql

update tablename
set
bfield = 25
where bfield = 20

if the sumfield cfield should be calculated also

update tablename
set
bfield = 25,
cfield = afield+25
where bfield = 20

hope this helps

meikl ;-)


0
 
LVL 10

Accepted Solution

by:
Jacco earned 100 total points
ID: 6922605
If you want to do this without looping through the records you will need an order field (I have called it bOrder):

1, 0  , 20 , 20
2, 20 , 25 , 45
3, 45 , 10 , 55

I dont know the DbiSam SQL dialect but normal SQL dialects support the following:

1) update the table with 25

update
  table
set
  bField = 25
where
  bOrder = 1

2) update all of the tables aFields and cFields

update
  table a
set
  aField = (select sum(bField) from table b where b.bOrder < a.bOrder),
  cField = (select sum(bField) from table b where b.bOrder <= a.bOrder)

* You might get NULL in sum fields though. Check DbiSam documentation if the have an IfNull function and then have something like IfNull(sum(bField), 0, sum(bField)).

Downside of this is the with a large table this can be a lengthy procedure. You can optimize it a bit using:

1) update the table with 25

update
  table
set
  bField = :newval
where
  bOrder = :index

2) update all of the tables aFields and cFields

update
  table a
set
  aField = (select sum(bField) from table b where b.bOrder < a.bOrder),
  cField = (select sum(bField) from table b where b.bOrder <= a.bOrder)
where
  bOrder >= :index

This will be a bit quicker in most cases.

I would like to mention as well that you are storing redundant data. This get one in to trouble very easily in database design. Another way to go would be:

1, 20
2, 25
3, 10

And use the following select:

select
  (select sum(bField) from table b where b.bOrder < a.bOrder) as aField,
  bField,
  (select sum(bField) from table c where c.bOrder <= a.bOrder) as cField
from
  table a
order by
  bOrder

This way you sums are always calculated "just in time" while selecting them. If you update one value of bField you just need to open/close the select query again.

Hope this helps,

Regards Jacco
0
 

Author Comment

by:InterDive
ID: 6922774
Thanxs Jacco :-)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

21 Experts available now in Live!

Get 1:1 Help Now