Solved

SQL Update table

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10, DigitalPersona Fingerprint reader and MySQL 6 222
PDF library for Delphi 2 107
Downloading email attachments 2 67
Magic Software info 18 125
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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

12 Experts available now in Live!

Get 1:1 Help Now