Solved

SQL Update table

Posted on 2002-03-30
3
193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month7 days, 17 hours left to enroll

617 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