update能同时更新两个以上字段吗?(通过一个表更新另一个表)(100分)

  • 主题发起人 coolnerd
  • 开始时间
C

coolnerd

Unregistered / Unconfirmed
GUEST, unregistred user!
环境:Oracle8i
问题:如何用一个表中的数据更新另一个表中的相关数据

描述:

有两个表:
table1(id,v1,v2)
table2(id,s1,s2)

如果只更新一个字段,没有问题:
update table1 a set v1=(select s1 from table2 b where a.id=b.id )

现在如果想同时更新两个字段,该如何操作呢?
也就是说让 table1.v1=table2.s1
table1.v2=table2.s2
同时进行,当然条件是table1.id=table2.id
 
分两次更新
 
update table1
set v1=s1, v2=s2
from table2
where table1.id = table2.id
 
[green]to: Admy[/green]
哎,分n次操作,这个自然不用提。
问题是如果字段很多,就麻烦了。

[green]to: tseug[/green]
好象不能这么写吧! 确定? Oracle? 反正刚刚实验过,sql语句报错。
----------------------------------------------------------------
如果是直接赋值,
update table set v1=1,v2=2,v3=3..... 这没有问题
可是想不通如果值是另一张表中的,如果能类似上面那样实现呢?
 
我不是已经回答了么
 
to: tseug
好象不能这么写吧! 确定? Oracle? 反正刚刚实验过,sql语句报错。
 
求Oracle的SQL语句,等待中。。。。。。。。
 
update table1
set v1=b.s1, v2=b.s2
from table1 as a,table2 as b
where a.id = b.id
 
可能是这样, 我手头没有Oracle, 你试试看
update table1 a set (v1, v2)=(select s1, s2 from table2 b where a.id=b.id )
 
to:hebohb 你的写法有问题,至少对Oracle

to:tseug ok. 想了半天就没有这么想,现在看看,多自然啊 [:D]
谢谢了.
 
不好意思,忘加分了 :)
 
试试这样行不行:

update table1
set t1.v1=t2.s1, t1.v2=t2.s2
from table1 t1,table2 t2
where t1.id = t2.id
 
顶部