没看懂楼主的意思!
不过我用过个种写法!不知道是否和楼主想的东东一样:
UPDATE T1 A SET (A.NAME,A.AGE) = (SELECT B.NAME,B.AGE FROM T2 B WHERE B.ID = A.ID)
后面还可以加上对T1表的WHERE更新条件!
不过我用过个种写法!不知道是否和楼主想的东东一样:
UPDATE T1 A SET (A.NAME,A.AGE) = (SELECT B.NAME,B.AGE FROM T2 B WHERE B.ID = A.ID)
后面还可以加上对T1表的WHERE更新条件!
buhuiyang(杨言不悔)说的这种格式.
没有值的话,原来的值全部清空,比如原来是5 ,查处来 B.NAME=6可以更新,则A.NAME=6,其他不符合条件的,原来值是3,全部清为空!代码如下,请各位帮忙看看.谢谢上面的两位!
update db_code_build a set cur_no =
(SELECT b.ch_value
FROM (select ch_value, ch_flag
from (select max(dj_no) as ch_value, ch_flag
from (select *
from (select RECEIPTS_ID ||
station_id || cur_month as ch_flag
from DB_CODE_BUILD)) u,
(select *
from (select substr(CH_ID, 1, 13) as DJ_ID,
substr(CH_ID, 14, 5) as dj_no
from YW_TRAIN_CHANGE)) T
where u.ch_flag = T.dj_id
group by ch_flag) tempTable,
DB_CODE_BUILD
where tempTable.ch_value > db_code_build.cur_no
and substr(ch_flag, 1, 2) =
DB_CODE_BUILD.receipts_id
and substr(ch_flag, 3, 5) =
DB_CODE_BUILD.station_id
and substr(ch_flag, 8, 6) =
DB_CODE_BUILD.cur_month) b
where a.receipts_id||a.station_id||a.cur_month = b.ch_flag)
(SELECT nvl(b.ch_value,null)
FROM (select ch_value, ch_flag
from (select max(dj_no) as ch_value, ch_flag
from (select *
from (select RECEIPTS_ID ||
station_id || cur_month as ch_flag
from DB_CODE_BUILD)) u,
(select *
from (select substr(CH_ID, 1, 13) as DJ_ID,
substr(CH_ID, 14, 5) as dj_no
from YW_TRAIN_CHANGE)) T
where u.ch_flag = T.dj_id
group by ch_flag) tempTable,
DB_CODE_BUILD
where tempTable.ch_value > db_code_build.cur_no
and substr(ch_flag, 1, 2) =
DB_CODE_BUILD.receipts_id
and substr(ch_flag, 3, 5) =
DB_CODE_BUILD.station_id
and substr(ch_flag, 8, 6) =
DB_CODE_BUILD.cur_month) b
where a.receipts_id||a.station_id||a.cur_month = b.ch_flag)
如果select得到多条记录的话,就会有问题的.
这样就要用批量更新MERGE 可以用来搞定
update db_code_build a
set cur_no = (
SELECT b.ch_value
FROM (select ch_value, ch_flag
from (select max(dj_no) as ch_value, ch_flag
from (select *
from (select RECEIPTS_ID || station_id ||
cur_month as ch_flag
from DB_CODE_BUILD)) u,
(select *
from (select substr(CH_ID, 1, 13) as DJ_ID,
substr(CH_ID, 14, 5) as dj_no
from YW_TRAIN_CHANGE)) T
where u.ch_flag = T.dj_id
group by ch_flag) tempTable,
DB_CODE_BUILD
where tempTable.ch_value > db_code_build.cur_no
and substr(ch_flag, 1, 2) =
DB_CODE_BUILD.receipts_id
and substr(ch_flag, 3, 5) =
DB_CODE_BUILD.station_id
and substr(ch_flag, 8, 6) = DB_CODE_BUILD.cur_month) b
where a.receipts_id || a.station_id || a.cur_month =
b.ch_flag)
where a.receipts_id || a.station_id || a.cur_month in
(select c.ch_flag
from (select ch_value, ch_flag
from (select max(dj_no) as ch_value, ch_flag
from (select *
from (select RECEIPTS_ID || station_id ||
cur_month as ch_flag
from DB_CODE_BUILD)) u,
(select *
from (select substr(CH_ID, 1, 13) as DJ_ID,
substr(CH_ID, 14, 5) as dj_no
from YW_TRAIN_CHANGE)) T
where u.ch_flag = T.dj_id
group by ch_flag) tempTable,
DB_CODE_BUILD
where tempTable.ch_value > db_code_build.cur_no
and substr(ch_flag, 1, 2) = DB_CODE_BUILD.receipts_id
and substr(ch_flag, 3, 5) = DB_CODE_BUILD.station_id
and substr(ch_flag, 8, 6) = DB_CODE_BUILD.cur_month) c)