2014-10-06 38 views
0

我試圖將某些表遷移到現有的表中,我只需要執行更新,其中DET_ATTACHMENT_ID等於DET_ATTACHMENT.ID,這裏是我迄今爲止的查詢。更新來自oracle中的連接表的行

UPDATE DET_ATTACHMENT 
     SET attachment_type = 'LAB', -- being added by the query, to replace the table difference 
      payer_criteria_id = (
      SELECT PAYER_CRITERIA_ID 
      FROM DET_LAB_ATTACHMENT 
      WHERE DET_LAB_ATTACHMENT.DET_ATTACHMENT_ID = DET_ATTACHMENT.ID) 
     WHERE exists(
       SELECT DET_ATTACHMENT_ID 
       FROM DET_ATTACHMENT 
        JOIN DET_LAB_ATTACHMENT ON (ID = DET_ATTACHMENT_ID) 
       WHERE DET_ATTACHMENT_ID = DET_ATTACHMENT.ID 

與現有查詢的問題是,它的設置行,從而具有「LAB」的attachment_type和歸零了payer_criteria_id它不匹配。我究竟做錯了什麼?

+0

你缺少attachment_type =在哪裏「LAB」條件存在條款 – radar 2014-10-06 19:56:45

+0

@Rajesh不存在,直到此更新 – xenoterracide 2014-10-06 19:59:14

+0

@xenoterracide,你可以這樣在where子句中存在選擇DET_ATTACHMENT_ID FROM DET_ATTACHMENT JOIN DET_LAB_ATTACHMENT ON DET_ATTACHMENT_ID = DET_ATTACHMENT.ID – radar 2014-10-06 20:03:09

回答

0

問題可能exists(...)謂詞始終計算爲true,從而爲det_attachment所有行update運行。試着這樣說:

UPDATE DET_ATTACHMENT X 
SET X.attachment_type = 'LAB', 
    X.payer_criteria_id = (
     SELECT C.PAYER_CRITERIA_ID 
     FROM DET_LAB_ATTACHMENT C 
     WHERE C.DET_ATTACHMENT_ID = X.ID 
    ) 
WHERE 
    exists(
     SELECT 1 
     FROM DET_ATTACHMENT A 
      JOIN DET_LAB_ATTACHMENT B 
       ON B.DET_ATTACHMENT_ID = A.ID 
      where B.det_attachment_id = X.id 
    ) 
;