SQL语句:查询多表更新数据

来源:互联网 发布:天涯明月刀ol导入数据 编辑:程序博客网 时间:2024/05/20 18:52

将 tbmdf05 的f80字段更新为 tbmdf03 和tbmdf03f的3个字段之和:

Update tbmdf05 Set tbmdf05.f80=
(
Select b.f1210+c.f1710+c.f1810
from tbmdf03 b,tbmdf03f c
where tbmdf05.sProjID=b.sProjID and tbmdf05.iDegree=b.iDegree
and tbmdf05.sProjID=c.sProjID and tbmdf05.iDegree=c.iDegree);

=============================

果然有效,看我的例子:

SQL> select * from t1;

S1 S2
---------- ----------
1 A
2 B
3 C

SQL> select * from t2;

S1 S2
---------- ----------
3 X
2 Y
1 Z

SQL> update t1 set s2=(select s2 from t2 where t1.s1=t2.s1);

已更新3行。

SQL> select * from t1;

S1 S2
---------- ----------
1 Z
2 Y
3 X


路虽远,行则必达;
事虽难,有为乃成!

-------------------------------------------

Update tProjInfo Set tProjInfo.sProjID=(Select substr(tProjInfo.sProjID,1,3) || '000' || substr(c.sMemo1,7,1) || substr (tProjInfo.sProjID,8,6) || substr(c.sMemo1,8,3) || substr (tProjInfo.sProjID,17,3) as sProjIDTemp  From (Select sProjID,iDegree,sDirectionCode,sMemo1 From tProjInfo Where length(sMemo1)=12 and substr(sMemo1,11,2)='00') VF0 Where tProjInfo.iDegree=VF0.iDegree AND substr (tProjInfo.sDirectionCode,1,length(VF0.sDirectionCode))=VF0.sDirectionCode);

报如下错误:
*
ERROR 位于第 1 行:
ORA-01407: 无法更新 ("YSZC115"."TPROJINFO"."SPROJID") 为 NULL


修改:
通过给tProjInfo也加相同的条件 length(tProjInfo.sMemo1)=12 and substr(tProjInfo.sMemo1,11,2)='00',使两个表tProjInfo,VF0有相同的记录数,才可更新,如下:

Update tProjInfo Set tProjInfo.sProjID=(Select substr(tProjInfo.sProjID,1,3) || '000' || substr(c.sMemo1,7,1) || substr (tProjInfo.sProjID,8,6) || substr(c.sMemo1,8,3) || substr (tProjInfo.sProjID,17,3) as sProjIDTemp  From (Select sProjID,iDegree,sDirectionCode,sMemo1 From tProjInfo Where length(sMemo1)=12 and substr(sMemo1,11,2)='00') VF0 Where tProjInfo.iDegree=VF0.iDegree AND substr (tProjInfo.sDirectionCode,1,length(VF0.sDirectionCode))=VF0.sDirectionCode)Where length(tProjInfo.sMemo1)=12 and substr(tProjInfo.sMemo1,11,2)='00';

------------------------------

你自己不是已经写了么?
使两个表tProjInfo,VF0有相同的记录数,才可更新
其实应该保证外面的记录数不多于里面查询的记录数

Update Set F1=({0}) where {1}
在你上面开始的那句Sql里,{0}处返回记录数可能少于{1}查询的记录数,因为{0}处对tProjInfo有限制length(sMemo1)=12 and substr(sMemo1,11,2)='00')
这样外面SQL里可能存在一条以上记录,它并不满足条件length(sMemo1)=12 and substr(sMemo1,11,2)='00')
但是依然在外面SQL返回的游标里
该记录在里面的SQL 联合查询时为NULL
更新变成了: Update tProjInfo Set sProjID=NULL WHERE rowid=...
而sProjID是NotNull的所以报错

------------------------------------------------------------------

update tbmda03F3 t3,tbmda03F1 t1 set t3.F0401=t3.F0101/t1.f0101, t3.F0402=t3.F0102/t1.f0102, t3.F0406=t3.F0106/t1.f0106, t3.F0407=t3.F0107/t1.f0107,
t3.F0408=t3.F0108/t1.f0108, t3.F0409=t3.F0109/t1.f0109, t3.F0410=t3.F0110/t1.f0110, t3.F0411=t3.F0111/t1.f0111, t3.F0412=t3.F0112/t1.f0112, t3.F0413=t3.F0113/t1.f0113, t3.F0414=t3.F0114/t1.f0114, t3.F0415=t3.F0115/t1.f0115, t3.F0416=t3.F0116/t1.f0116 where t1.sprojid=t3.sprojid and t1.idegree=t3.idegree

 

update tprojinfor t1 set sprojid=(select sprojid from tprojinfor t2 where t2.smemo1='3' and substr(t2.smostid,1,4)='2006' and t1.smostid=t2.smostid) where t1.smemo1='2' and substr(t1.smostid,1,4)='2006' and smostid in (select smostid from tprojinfor t3 where t3.smemo1='3' and substr(t3.smostid,1,4)='2006');

原创粉丝点击