在oracle中更新连接表中的行

时间:2014-10-06 19:43:29

标签: sql oracle oracle11g

我尝试将某些表迁移到现有表中,我只需要在DET_ATTACHMENT_ID等于DET_ATTACHMENT.ID的情况下执行更新,这是我到目前为止的查询。

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

现有查询的问题在于,它将每个行设置为attachment_type" LAB",并将{{{ 1}}它没有匹配。我做错了什么?

1 个答案:

答案 0 :(得分:0)

问题可能,您的exists(...)谓词始终评估为true,从而为update的所有行运行det_attachment。试试这种方式:

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
    )
;