Solved

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

Posted on 2000-04-27
3
382 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
[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
  • 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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
control image tags in a string ? 12 153
FMX and jaudiotracker playing memory stream 29 98
Mutiple Folder view (5 Folder view) 3 54
Firemonkey Queue list 1 32
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…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
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 …

726 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