我有一个源表和目标表。我正在做一个简单的合并,我可以同时进行插入和更新操作。我刚刚添加了插入日期和更新日期等列,以了解何时插入和更新记录。然后,我还添加了一个标志列,告诉我们如果记录是否更新,如果是,那么'Y'否则'N'。我成功,直到这个,但当我尝试使用删除它不允许我。我在sql server中看到了很多解决方案,但我的是oracle。我正在进行软删除,其中记录不会从目标中删除,但如果从源表中删除了某些记录,则只将标志列更新为“Y”。但我无法做到这一点。我也使用了case但没有用。要清楚我只想在源代码中删除时更新标志。任何人都能告诉我如何用单合并声明来做到这一点?我有简单的表学生有名称,地址移动否,状态等属性。我这样做了我的查询 -
merge into target t using (SELECT CASE WHEN S.STUDENT_ID IS NULL THEN T.STUDENT_ID ELSE S.STUDENT_ID END
AS STUDENT_ID,S.NAME,S.ADRESS,S.STATE,S.MOBILE FROM STUDENTS S full JOIN TARGET T ON S.STUDENT_ID=T.STUDENT_ID) s
on (s.student_id = t.student_id)
when not matched then insert (student_id,name,address,state,mobile,insert_date,update_date)
values(s.student_id,s.name,s.adress,s.state,s.mobile,sysdate,sysdate)
when matched then update set t.name=s.name,t.address=s.adress, t.state=s.state,t.mobile=s.mobile,update_date=sysdate
where s.name!=t.name or s.adress!=t.address or s.state!=t.state or s.mobile!=t.mobile;
这次我使用了每个属性的case并且我能够解决它但是每次运行它都会显示目标合并中存在的额外行没有 - 4行被合并我不希望任何人都可以帮我查询为 -
MERGE INTO TARGET T USING (SELECT CASE WHEN S.STUDENT_ID IS NULL THEN T.STUDENT_ID ELSE S.STUDENT_ID END
AS STUDENT_ID,S.NAME,S.ADRESS,S.STATE,S.MOBILE FROM STUDENTS S FULL JOIN TARGET T ON S.STUDENT_ID=T.STUDENT_ID) S
ON (S.STUDENT_ID=T.STUDENT_ID)
WHEN NOT MATCHED THEN INSERT
(STUDENT_ID,NAME,ADDRESS,STATE,MOBILE,INSERT_DATE,UPDATE_DATE,FLAG )
VALUES(S.STUDENT_ID,S.NAME,S.ADRESS,S.STATE,S.MOBILE,SYSDATE,SYSDATE, 'N')
WHEN MATCHED THEN UPDATE SET T.FLAG='Y' , t.name = case when s.name is null then T.NAME else S.name end ,
T.ADDRESS=case when s.ADRESS is null then T.ADDRESS else S.ADRESS end ,
t.STATE=case when s.STATE is null then T.STATE else S.STATE end ,
T.MOBILE=CASE WHEN S.MOBILE IS NULL THEN T.MOBILE ELSE S.MOBILE END
WHERE S.NAME!=T.NAME OR S.ADRESS!=T.ADDRESS OR S.STATE!=T.STATE OR S.MOBILE!=T.MOBILE or t.STUDENT_ID in
(SELECT TARGET.STUDENT_ID FROM
STUDENTS FULL JOIN TARGET ON STUDENTS.STUDENT_ID=TARGET.STUDENT_ID WHERE STUDENTS.STUDENT_ID IS NULL);
答案 0 :(得分:0)
回答我的问题 -
MERGE INTO TARGET T USING
(SELECT CASE
WHEN S.STUDENT_ID IS NULL THEN T.STUDENT_ID
ELSE S.STUDENT_ID
END AS STUDENT_ID,
S.NAME,
S.ADRESS,
S.STATE,
S.MOBILE
FROM STUDENTS S
FULL JOIN TARGET T ON S.STUDENT_ID=T.STUDENT_ID) S ON (S.STUDENT_ID=T.STUDENT_ID) WHEN NOT MATCHED THEN
INSERT (STUDENT_ID,
NAME,
ADDRESS,
STATE,
MOBILE,
INSERT_DATE,
UPDATE_DATE,
FLAG)
VALUES(S.STUDENT_ID,
S.NAME,
S.ADRESS,
S.STATE,
S.MOBILE,
SYSDATE,
SYSDATE,
'N') WHEN MATCHED THEN
UPDATE
SET T.FLAG= CASE
WHEN ( T.STUDENT_ID IN (SELECT TARGET.STUDENT_ID
FROM STUDENTS
FULL JOIN TARGET ON STUDENTS.STUDENT_ID=TARGET.STUDENT_ID
WHERE STUDENTS.STUDENT_ID IS NULL)) THEN 'Y'
ELSE 'N'
END,
T.NAME = CASE
WHEN S.NAME IS NULL THEN T.NAME
ELSE S.NAME
END,
T.ADDRESS=CASE
WHEN S.ADRESS IS NULL THEN T.ADDRESS
ELSE S.ADRESS
END,
T.STATE=CASE
WHEN S.STATE IS NULL THEN T.STATE
ELSE S.STATE
END,
T.MOBILE=CASE
WHEN S.MOBILE IS NULL THEN T.MOBILE
ELSE S.MOBILE
END,
T.UPDATE_DATE=SYSDATE ,
T.INSERT_DATE=SYSDATE
WHERE S.NAME!=T.NAME
OR S.ADRESS!=T.ADDRESS
OR S.STATE!=T.STATE
OR S.MOBILE!=T.MOBILE
OR T.STUDENT_ID IN
(SELECT TARGET.STUDENT_ID
FROM STUDENTS
FULL JOIN TARGET ON STUDENTS.STUDENT_ID=TARGET.STUDENT_ID
WHERE STUDENTS.STUDENT_ID IS NULL)
AND FLAG!='Y';