| |

VerySource

 Forgot password?
 Register
Search
View: 2430|Reply: 11

How can the following while code be optimized? (Modify the existing quantity in the inventory table after storage)

[Copy link]

2

Threads

12

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-5 00:30:01
| Show all posts |Read mode
// Modify the existing quantity in the inventory table
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add ('Select * from RukuTmp');
ADOQuery1.Open;
// Find all data from the temporary table
ADOQuery1.First;
while not ADOQuery1.Eof do
begin
  ADOQuery2.Close;
  ADOQuery2.SQL.Clear;
  ADOQuery2.SQL.Add ('Select * from Kucun where ChID =: a');
  ADOQuery2.Parameters.ParamByName ('a'). Value: = ADOQuery1.fieldbyname ('ChID'). AsString;
  ADOQuery2.Open;
  ADOQuery2.Edit;
  ADOQuery2.FieldByName ('Kcsl'). AsFloat: = ADOQuery2.fieldbyname ('Kcsl'). AsFloat
    + ADOQuery1.fieldbyname ('Rksl'). AsFloat;
  ADOQuery2.Post;
  ADOQuery1.Next;
end;
Reply

Use magic Report

2

Threads

12

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-5 01:48:01
| Show all posts
Can I modify the data in the inventory table at one time without using a while loop?
Reply

Use magic Report

0

Threads

40

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-1-5 08:57:01
| Show all posts
update Ku village set KC SL = Ku village.KC SL + Ru library tmp.RK died from Ku village, Ru library tmp where Ku village. CH ID = Ru library tmp.CH ID
Reply

Use magic Report

2

Threads

12

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-5 10:36:01
| Show all posts
Both the inventory quantity and inventory amount can be changed like this
update Kucun
set Kcsl = Kucun.Kcsl + RukuTmp.Rksl, Kcje = Kucun.Kcje + RukuTmp.Rkje
from Kucun, RukuTmp
where Kucun.ChID = RukuTmp.ChID

But what about the unit price of inventory?
The inventory unit price is obtained by dividing the inventory amount by the inventory quantity.
update Kucun set Kcdj = Kcje / Kcsl from Kucun, RukuTmp
where Kucun.ChID = RukuTmp.ChID and Kucun.Kcsl <> 0 (inventory quantity is not 0)
update Kucun set Kcdj = 0 from Kucun, RukuTmp
where Kucun.ChID = RukuTmp.ChID and Kucun.Kcsl = 0 (the stock quantity is 0)
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-5 11:42:01
| Show all posts
Sum with SUM
with ADOQuery1 do
begin
  close;
  sql.clear;
  sql.add ('update Kucun set Kcsl = c.Kcsl from (select ChID, sum (Kcsl) as Kcsl from RukuTmp group by ChID) c, warehouse where Kucun.ChID = c.ChID');
  execsql;
end;
Reply

Use magic Report

2

Threads

12

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-5 13:03:01
| Show all posts
xclzc17()
I tried, your method is wrong, you cannot join two tables after Update.
Reply

Use magic Report

0

Threads

40

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-1-5 13:48:01
| Show all posts
What database? What does it mean that two tables cannot be joined?
Reply

Use magic Report

2

Threads

12

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-5 14:12:01
| Show all posts
I am an Access database, and I am prompted at runtime:
Syntax error (operator missing) in query expression 'Kucun.Kcsl + RukuTmp.Rksl from Kucun'.
Reply

Use magic Report

2

Threads

12

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-5 14:21:01
| Show all posts
So I judge it is ', RukuTmp' is not recognized by the program, can I only follow one table after the update?
Reply

Use magic Report

0

Threads

40

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-1-5 16:30:01
| Show all posts
Well, it's possible that Access does not support this SQL syntax. You can try the四月天method.

If it doesn't work, you can use a loop
ADOQuery1.Open;
// Find all data from the temporary table
ADOQuery1.DisableControls; // Add this sentence
try
ADOQuery1.First;
ADOQuery2.Close;
ADOQuery2.SQL.Clear;
ADOQuery2.SQL.Add ('update Kucun set Kcsl = Kcsl +: Rksl where ChID =: ChID');
while not ADOQuery1.Eof do
begin
  ADOQuery2.Parameters.ParamByName ('Rksl'). Value: = ADOQuery1.fieldbyname ('Rksl'). AsFloat;
ADOQuery2.Parameters.ParamByName ('ChID'). Value: = ADOQuery1.fieldbyname ('ChID'). AsString;
ADOQuery2.ExecSQL

  ADOQuery1.Next;
end;
finally
ADOQuery1.EnableControls;
end;
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list