Solved

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

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Many companies are making the switch from Microsoft to Google Apps (https://www.google.com/work/apps/business/). Use this article to learn more about what Google Apps has to offer and to help if you’re planning on migrating to Google Apps. It is …
The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
Viewers will learn the different options available in the Backstage view in Excel 2013.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

911 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

23 Experts available now in Live!

Get 1:1 Help Now