日期:2014-05-17  浏览次数:21002 次

求助:在PL/SQL编写过程,实现利用一个表中的数据更新另外一个表中的数据
以前没有使用过PL/SQL编过过程,第一次,求大家详细点。还有点分,都捐出来吧。
数据表biao1和biao2。
biao1(
id_man,字符型
rq,日期型
sale 数值型
);
biao2(
id_man,字符型
sale 数值型
);
、、、、、、、、、、、、、
现在需要对biao2中所有记录的sale字段,进行更新。
利用biao1中id_man值相同(即biao1.id_man=biao2.id_man)、rq为当日日期的sale相应的值。
------解决方案--------------------
merge into biao2 b2
using biao1 b1
on (b1.id_man=b2.id_man)
when matchd then
update set
 b2.sale =b1.sale 

------解决方案--------------------
 update biao2 b 
 set b.sale=(select nb.sale from (select b1.id_man,b1.sale from biao1 b1 join biao2 b2 on b1.id_man=b2.id_man where to_char(b1.rq,'yyyy-mm-dd')=to_char((select sysdate from dual),'yyyy-mm-dd')) nb where b.id_man=nb.id_man);

注意如果biao1没有biao2的id_man
------解决方案--------------------
declare 
  cursor cur_aa is select * from biao1;
begin
  for cur_a in cur_aa
    update biao2 set ... where id_man=cur_a.id_man and trunc(b1.rq)=trunc(sysdate);
  end loop;
end;

--It is a demo .
--if undo and temp tablespace are enough ,sql is more efficient than plsql
------解决方案--------------------
And don't forget to commit partly when the rows is large.
------解决方案--------------------
merge into biao2 b2
using (select * from biao1 where trunc(rq)=trunc(sysdate)) b1
on (b1.id_man=b2.id_man)
when matchd then
update set b2.sale =b1.sale;