我有一个源表,其中包含字段ID
、NAME
和DESIG
create or replace table emp_source
(
id number,
name string,
desig string
);
而我的目的表具有列ID
NAME
DESIG
META_CRT_DT
(插入记录的日期)META_UPD_DT
(更新记录的日期)META_IUD_FLG
(将根据执行的插入、删除或更新操作进行更新)CURRENT_FLAG
(如果删除记录,则必须将其设为FALSE)
create or replace table emp_des
(
id number,
name string,
desig string,
META_CRT_DT timestamp_ntz,
META_UPD_DT timestamp_ntz,
META_IUD_FLG char,
current_flag boolean
);
我正在插入这样的新唱片
INSERT INTO emp_des (id, name, desig,META_CRT_DT,
meta_upd_dt, meta_iud_flg, current_flag)
SELECT
id, name, desig,
to_date(current_timestamp) as crt,
to_date(current_timestamp) as upd,
'I' as iud, TRUE as flag
FROM
emp_source
WHERE
NOT EXISTS (SELECT * FROM emp_des
WHERE emp_source.id = emp_des.id);
当源表中的记录更新时,假设名称更改,在EMP_DES
表中,我需要将DESIG
、META_UPD_DT
和META_IUD_FLG
更新为‘U’.
同样,当删除一条记录时,我需要将META_UPD_DT
和META_IUD_FLG
更新为‘D’.
我是SQL的新手,所以如果有任何帮助,我将非常感激.谢谢.
当源表中的记录更新时,假设名称更改,在EMP_DES
表中,我需要将DESIG
、META_UPD_DT
和META_IUD_FLG
更新为‘U’. I tried this but it changes all flags to 'U'.
UPDATE emp_des
SET desig = s.desig,
meta_upd_dt = to_date(current_timestamp),
meta_iud_flg = 'U',
current_flag = 'TRUE'
FROM emp_source AS s
WHERE EXISTS (SELECT * FROM emp_des WHERE id = emp_des.id);
同样,当删除一条记录时,我需要将META_UPD_DT
和META_IUD_FLG
更新为‘D’.
我是SQL的新手,所以我想让它尽可能简单.感谢您抽出时间来.