合并不会更新记录

时间:2018-01-21 00:03:00

标签: database oracle merge

我正在尝试使用MERGE为我的scdthree_dim_placement表创建当前和以前的记录。

当我尝试更新plct_desc clean_placement previous_plct_desc scdthree_dim_placement中的current_plct_desc时,drop table scdthree_dim_placement; create table scdthree_dim_placement ( scdplacement_id number(8), placement_key number (8), current_plct_desc varchar2(50), previous_plct_desc varchar2(50), plct_start_date date, PRIMARY KEY (scdplacement_id) ); drop sequence plctscd_seq; CREATE SEQUENCE plctscd_seq START WITH 1; CREATE OR REPLACE TRIGGER plct_bir BEFORE INSERT ON scdthree_dim_placement FOR EACH ROW BEGIN SELECT plctscd_seq.NEXTVAL INTO :new.scdplacement_id FROM dual; END; / 显示没有任何内容,merge into scdthree_dim_placement a using clean_placement b on (a.PLACEMENT_KEY=b.PLACEMENT_ID) WHEN matched THEN UPDATE SET a.current_plct_desc = b.plct_desc, a.previous_plct_desc = a.current_plct_desc, a.plct_start_date = b.plct_start_date WHEN NOT MATCHED THEN INSERT ( a.placement_key, a.current_plct_desc, a.plct_start_date ) VALUES( b.placement_id, b.plct_desc, b.plct_start_date ); 列也无法正常工作。

运行脚本时没有显示错误。我是初学者,如果有人能告诉我我做错了什么,那将是一个很大的帮助。

info = [
    'Joe Schmoe',
    'W / M / 64',
    'Richard Johnson',
    'OFFICER',
    'W / M /48',
    'Adrian Stevens',
    '? / ? / 27'
    ]

这是我的MERGE声明:

Officer=False

2 个答案:

答案 0 :(得分:0)

two blank lines行与a.plct_start_date=b.plct_start_date行和WHEN NOT MATCHED THEN子句行之间的语法没有问题,请删除这些行。

如果来自序列的值和scdplacement_id(具有主键约束)的值发生任何重合,则可能会出现主键违规问题。

答案 1 :(得分:0)

--Code looks fine. Just Remove Extra Space

drop table scdthree_dim_placement;
create table scdthree_dim_placement
(
scdplacement_id number(8),
placement_key number (8),
current_plct_desc varchar2(50),
previous_plct_desc varchar2(50),
plct_start_date date,
PRIMARY KEY (scdplacement_id)
);
drop sequence plctscd_seq;
CREATE SEQUENCE plctscd_seq START WITH 1;

CREATE OR REPLACE TRIGGER plct_bir
BEFORE INSERT ON scdthree_dim_placement
FOR EACH ROW
BEGIN
  SELECT plctscd_seq.NEXTVAL
  INTO   :new.scdplacement_id
  FROM   dual;
END;
/

merge into scdthree_dim_placement a using clean_placement  b on
       (a.PLACEMENT_KEY=b.PLACEMENT_ID)
WHEN matched
THEN
 UPDATE SET
a.current_plct_desc=b.plct_desc,
a.previous_plct_desc=a.current_plct_desc,
a.plct_start_date=b.plct_start_date
WHEN NOT MATCHED THEN
INSERT (
a.placement_key,
a.current_plct_desc,
a.plct_start_date
)
VALUES(
b.placement_id,
b.plct_desc,
b.plct_start_date
);