这是sql语句:
UPDATE 
(SELECT table1.nbqe as OLD_nbqe, table2.nbqe as NEW_nbqe,
  table1.adr1 as OLD_adr1, table2.adr1 as NEW_adr1
  table1.adr3 as OLD_adr3, table2.adr2 as NEW_adr3
 FROM table1
 INNER JOIN table2
 ON table1.cg= table2.cg AND table1.ce = table2.ce
) t
SET t.OLD_nbqe = t.NEW_nbqe, t.OLD_adr1 = t.NEW_adr1, t.OLD_adr3 = t.NEW_adr3
这会发生 01779 错误:无法修改映射到非键保留表的列
如何修改sql语句来实现操作?
泰
注意:这个问题不重复
table1 columns :
 nbqe
 adr1
 adr2
 adr3
 cg
 ce
table2 column :
 nbqe
 adr1
 adr2
 cg
 ce
在表2中,cg + ce是单个键
在表 1 中,您可以拥有同一对 (cg、ce) 的多个记录。
这些表没有任何约束,甚至没有主键或任何东西。
我会以不同的方式问它。sql语句可能是错误的。
更新中的选择返回表 1 的 8 行,这些行应该使用 t 表中可以找到的 table2 的值进行修改。
如何使用表 t 更新 table1 的 8 行与 table2 的相应值:是否可能,或者我应该编写一个大的 sql 语句,一直重复相同的子查询,这似乎非常奇怪且不干净。
table2如果对应的行不超过一行,table1则使用merge:
merge into table1 d
using table2 s
on (d.cg = s.cg and d.ce = s.ce)
when matched then update set
    d.nbqe = s.nbqe, d.adr1 = s.adr1, d.adr3 = s.adr3
只有匹配的行才会被更新,其余的保持不变。您可以使用 来做到这一点update,但这样您就可以避免重复where子句的条件。
测试:
create table table1 (nbqe int, adr1 int, adr2 int, adr3 int, cg int, ce int);
create table table2 (nbqe int, adr1 int, adr2 int, adr3 int, cg int, ce int);
insert into table1 values (1, 1, 1, 1, 1, 1);
insert into table1 values (2, 2, 2, 2, 2, 2);
insert into table1 values (3, 3, 3, 3, 2, 2);
insert into table2 values (5, 5, 5, 5, 2, 2);
结果:
NBQE ADR1 ADR2 ADR3   CG   CE
---- ---- ---- ---- ---- ----
   1    1    1    1    1    1
   5    5    2    5    2    2
   5    5    3    5    2    2
| 归档时间: | 
 | 
| 查看次数: | 5234 次 | 
| 最近记录: |