Solved

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

Posted on 2000-04-27
3
369 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Abstract this sub 6 55
Delphi IDE crash without error message ... 7 60
Magic Software info 18 105
tidtcpserver connection lost handle 2 47
The canonical version of this article is on my web site here: http://iconoun.com/articles/collisions/ A companion presentation is available here: http://iconoun.com/articles/collisions/Unicode_Presentation.pdf
We were having a lot of "Heartbeat Alerts" in our SCOM environment, now "Heartbeat" in a SCOM environment for those of you who might not be familiar with SCOM is a packet of data sent from the agent to the management server on a regular basis, basic…
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

757 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

21 Experts available now in Live!

Get 1:1 Help Now