Oracle에서 Inner Join과 함께 업데이트 문
-
20-09-2019 - |
문제
MySQL에서 잘 작동하는 쿼리가 있지만 Oracle에서 실행하면 다음 오류가 발생합니다.
SQL 오류 : ORA-00933 : SQL 명령이 제대로 종료되지 않았습니다.
00933. 00000- "SQL 명령이 제대로 종료되지 않았다"
쿼리는 다음과 같습니다.
UPDATE table1
INNER JOIN table2 ON table1.value = table2.DESC
SET table1.value = table2.CODE
WHERE table1.UPDATETYPE='blah';
해결책
해당 구문은 Oracle에서 유효하지 않습니다. 당신은 이것을 할 수 있습니다 :
UPDATE table1 SET table1.value = (SELECT table2.CODE
FROM table2
WHERE table1.value = table2.DESC)
WHERE table1.UPDATETYPE='blah'
AND EXISTS (SELECT table2.CODE
FROM table2
WHERE table1.value = table2.DESC);
아니면 당신 ~할 것 같다 이것을 할 수 있습니다 :
UPDATE
(SELECT table1.value as OLD, table2.CODE as NEW
FROM table1
INNER JOIN table2
ON table1.value = table2.DESC
WHERE table1.UPDATETYPE='blah'
) t
SET t.OLD = t.NEW
(인라인보기가 Oracle에 의해 업데이트 가능한 것으로 간주되는지 여부에 따라 다릅니다).
다른 팁
이것을 사용하십시오 :
MERGE
INTO table1 trg
USING (
SELECT t1.rowid AS rid, t2.code
FROM table1 t1
JOIN table2 t2
ON table1.value = table2.DESC
WHERE table1.UPDATETYPE='blah'
) src
ON (trg.rowid = src.rid)
WHEN MATCHED THEN UPDATE
SET trg.value = code;
MERGE
~와 함께 WHERE
절:
MERGE into table1
USING table2
ON (table1.id = table2.id)
WHEN MATCHED THEN UPDATE SET table1.startdate = table2.start_date
WHERE table1.startdate > table2.start_date;
당신은 필요합니다 WHERE
열이 참조 되었기 때문에 절 ON
절을 업데이트 할 수 없습니다.
UPDATE ( SELECT t1.value, t2.CODE
FROM table1 t1
INNER JOIN table2 t2 ON t1.Value = t2.DESC
WHERE t1.UPDATETYPE='blah')
SET t1.Value= t2.CODE
위의 답을 사용하지 마십시오.
어떤 사람들은 중첩 된 선택의 사용을 제안합니다. 그렇게하지 마십시오. 업데이트 할 레코드가 많으면 가입을 사용하십시오.
update (select bonus
from employee_bonus b
inner join employees e on b.employee_id = e.employee_id
where e.bonus_eligible = 'N') t
set t.bonus = 0;
자세한 내용은이 링크를 참조하십시오.http://geekswithblogs.net/willsmith/archive/2008/06/18/oracle-update-with-join-again.aspx.
또한 결합중인 모든 테이블에 기본 키가 있는지 확인하십시오.
표시된대로 여기, Tony Andrews가 제안한 첫 번째 솔루션의 일반적인 구문은 다음과 같습니다.
update some_table s
set (s.col1, s.col2) = (select x.col1, x.col2
from other_table x
where x.key_value = s.key_value
)
where exists (select 1
from other_table x
where x.key_value = s.key_value
)
특히 하나 이상의 필드를 업데이트하고 싶다면 이것이 흥미 롭다고 생각합니다.
이 다음 구문은 저에게 효과적입니다.
UPDATE
(SELECT A.utl_id,
b.utl1_id
FROM trb_pi_joint A
JOIN trb_tpr B
ON A.tp_id=B.tp_id Where A.pij_type=2 and a.utl_id is null
)
SET utl_id=utl1_id;
잘 작동합니다
merge into table1 t1
using (select * from table2) t2
on (t1.empid = t2.empid)
when matched then update set t1.salary = t2.salary
사용 설명 표 2의 DESC 대신
update
table1
set
value = (select code from table2 where description = table1.value)
where
exists (select 1 from table2 where description = table1.value)
and
table1.updatetype = 'blah'
;
UPDATE table1 t1
SET t1.value =
(select t2.CODE from table2 t2
where t1.value = t2.DESC)
WHERE t1.UPDATETYPE='blah';
UPDATE IP_ADMISSION_REQUEST ip1
SET IP1.WRIST_BAND_PRINT_STATUS=0
WHERE IP1.IP_ADM_REQ_ID =
(SELECT IP.IP_ADM_REQ_ID
FROM IP_ADMISSION_REQUEST ip
INNER JOIN VISIT v
ON ip.ip_visit_id=v.visit_id
AND v.pat_id =3702
); `enter code here`
완전성의 문제와 마찬가지로, 우리가 Oracle을 말하고 있기 때문에 이것은 또한 그렇게 할 수 있습니다.
declare
begin
for sel in (
select table2.code, table2.desc
from table1
join table2 on table1.value = table2.desc
where table1.updatetype = 'blah'
) loop
update table1
set table1.value = sel.code
where table1.updatetype = 'blah' and table1.value = sel.desc;
end loop;
end;
/
UPDATE (SELECT T.FIELD A, S.FIELD B
FROM TABLE_T T INNER JOIN TABLE_S S
ON T.ID = S.ID)
SET B = A;
A와 B는 별칭 필드이며 테이블을 가리킬 필요가 없습니다.
update table1 a
set a.col1='Y'
where exists(select 1
from table2 b
where a.col1=b.col1
and a.col2=b.col2
)