Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Update table

Posted on 2002-03-30
3
Medium Priority
?
201 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 400 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

Technology Partners: 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

722 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