Solved

Updating a dBase table based on values from a multi-table query: Delphi 4.0 Professional

Posted on 2000-04-27
3
377 Views
Last Modified: 2013-11-24
I need to update a field in a dBase table based upon calculations performed against several fields in different dBase tables. I can successfully update the table based on equations that use values that exist in the same table.  
e.g. UPDATE ''activity'' SET calc_act =id_src*1000
 
However  I need to alter the update sql  to use values from other tables.  
e.g. Update  ''activity'' SET calc_act =id_src*1000*Table2.field.value/Table3.field.value

 I need to alter the update to use values from other tables.  How do I accomplish this?  

 I can successfully join the tables using SQL and can display the result set in a tdbgrid.  But the result set does not appear to be "live" even though I have requested such.  So changes I enter to the grid do not transfer to the underlying tables.  Why?  How can I make this work?
0
Comment
Question by:Zachary
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
shenqw earned 500 total points
ID: 2758020
1.Use the follow line sql to update the table

update test
  set v=(select v from test2 where id=test.id)*(select v from test1 where id=test.id)
  where id in
       (select test1.id from test1 inner join test2 on test1.id=test2.id)


2. you must use TUpdateSQL.

i.  add tquery,tupdatesql1,tupdatesql2.....(many tupdatesqls as your table you want to update)
ii. set you tquery.sql.text:='select test1.id,test1.fieldvalue,test2.fieldvalue from table1 inner join table2 on table1 on table1.id=table2.id';
   and set TQuery.requestLive:=True;
iii. use tupdatesql1 to update table1. settings:
   tupdatesql1.deletesql.Text:='delete test1 where id=OLD_ID';
   tupdatesql1.insertsql.Text:='insert into test1 values(:NEW_ID,:NEW_V)';
   tupdatesql1.modifysql.Text:='UPDATE TEST1 SET V=:new_v where ID=:OLD_ID';
   
   the table2 and tupdatesql2 ......

iv. On TQuery.OnUpdateRecord event:

procedure TForm1.Query1UpdateRecord(DataSet: TDataSet;
  UpdateKind: TUpdateKind; var UpdateAction: TUpdateAction);
begin
  Query1.UpdateObject:=updatesql1;
  updatesql1.SetParams(UpdateKind);
  updatesql1.ExecSQL(UpdateKind);

  Query1.UpdateObject:=updatesql2;
  updatesql2.SetParams(UpdateKind);
  updatesql2.ExecSQL(UpdateKind);

  UpdateAction:=uaApplied;
end;


v. all of above,then you can do it like before in tdbgrid.

vi. don't modify the id value. or you will get wrong value.

//Based on NT4+DBase5+Delph5+Pack1.
//If you need , leave you e-mail, i will send the all project source code and the database.
0
 
LVL 3

Expert Comment

by:shenqw
ID: 2758090
Sorry ,something is wrong.

Here is a example that show how to use tupdatesql and cacheupdate.

2. you must use TUpdateSQL.

i.  add tquery,tupdatesql1,tupdatesql2.....(many tupdatesqls as your table you want to update)

ii. set you tquery.sql.text:='select test1.id,test1.v v1,test2.v v2 from table1 inner join table2 on table1 on table1.id=table2.id';
   and set TQuery.requestLive:=True; AND TQuery.CacheUpdate:=True;

iii. use tupdatesql1 to update table1. settings:
   tupdatesql1.deletesql.Text:='delete from test1 where id=:OLD_ID';
   tupdatesql1.insertsql.Text:='insert into test1(ID,V) values(:ID,:V1)';
   tupdatesql1.modifysql.Text:='UPDATE TEST1 SET V=:V1 where ID=:OLD_ID';
   
   the table2 and tupdatesql2 ......

   tupdatesql1.deletesql.Text:='delete from test2 where id=:OLD_ID';
   tupdatesql1.insertsql.Text:='insert into test2(ID,V) values(:ID,:V12)';
   tupdatesql1.modifysql.Text:='UPDATE TEST2 SET V=:V2 where ID=:OLD_ID';

iv. On TQuery.OnUpdateRecord event:

procedure TForm1.Query1UpdateRecord(DataSet: TDataSet;
  UpdateKind: TUpdateKind; var UpdateAction: TUpdateAction);
begin
  Query1.UpdateObject:=updatesql1;
  updatesql1.SetParams(UpdateKind);
  updatesql1.ExecSQL(UpdateKind);

  Query1.UpdateObject:=updatesql2;
  updatesql2.SetParams(UpdateKind);
  updatesql2.ExecSQL(UpdateKind);

  UpdateAction:=uaApplied;
end;


v. when you want to write the change into database ,do this

procedure TForm1.Button1Click(Sender: TObject);
begin
  Query1.ApplyUpdates;
  Query1.CommitUpdates;
end;

0
 

Author Comment

by:Zachary
ID: 2764894
Thanks alot!
And thanks for being so concise!
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

832 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