The name is empty? Type
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
BUYER_UID VARCHAR2 (6)
STORE_NO NUMBER (5)
SALE_AMOUNT NUMBER
SALE_AMOUNT_BUYER NUMBER
The MONTH NUMBER (2)
The YEAR NUMBER (4)
The cursor is as follows:
Real tsales;
String tbuyer;
Declare buy cursor for
The select buyer_uid, sum (sale_amount)
The from ng_store_sales
Where year=: lsyear and month=: lsmonth
Group by buyer_uid
;
The open buy;
The do while the sqlca. Sqlcode=0;
The fetch buy into: tbuyer, : tsales;
The update ng_store_sales set SALE_AMOUNT_BUYER=: tsales
Where buyer_uid=: tbuyer and year=: lsyear and month=: lsmonth;
The fetch buy into: tbuyer, : tsales;
Loop;
The close buy;
commit;
By buyer_uid collect ng_store_sales exterior-interior sale_amount field data, update sale_amount_buyer field
SQL> The select buyer_uid, sum (sale_amount)
2 the from ng_store_sales group by buyer_uid;
BUYER_ SUM (SALE_AMOUNT)
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
F1 1570627.17
F2 3445516.92
F3 763568.04
F4 715882.3
F5 5093196.31
After the cursor execution, and the results are as the following:
Select distinct buyer_uid, sale_amount_buyer from ng_store_sales;
BUYER_ SALE_AMOUNT_BUYER
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
F1 1570627.13
F2 3445517
F3 0
F4 715882.313
F5 0
Why the data update only half?
CodePudding user response:
Year=: lsyear and month=: lsmonthUpdate and year and the month, the last query, and without the two conditions, the results may be different, of course
CodePudding user response:
1, it is recommended that: you this completely without cursor, cursor to realize such a function is very slow, you can use an update statement to update2, the problem: you this cursor execution SQL problem may come up completely, open cursor, sqlcode 0, but can be fine when you fetch a sqlcode is not zero, of course you update statement may not success!
The open buy;
The do while the sqlca. Sqlcode=0;
The fetch buy into: tbuyer, : tsales;
If the sqlca. Sqlcode & lt;> 0 then
The rollback using sqlca;
MessageBox (" prompt ", "failure", stopSign!)
Return
End the if
The update ng_store_sales set SALE_AMOUNT_BUYER=: tsales
Where buyer_uid=: tbuyer and year=: lsyear and month=: lsmonth;
If the sqlca. Sqlcode & lt;> 0 then
The rollback using sqlca;
MessageBox (" prompt ", "failure", stopSign!)
Return
End the if
The fetch buy into: tbuyer, : tsales;
Loop;
The close buy;
CodePudding user response: