Solved

SQL Update table

Posted on 2002-03-30
3
187 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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

732 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