所以我有以下代码来创建2个表。第一个表是一个临时表,我将执行批量插入(NOLOGGING Sh-tuff省略)。第二张桌子是现场表格。
CREATE TABLE STG_TBL(
LIVE_TBL_ID NUMBER,
CNT NUMBER,
BATCH_ID NUMBER
)
PARTITION BY RANGE(BATCH_ID)
INTERVAL (1)
(PARTITION EMPTY VALUES LESS THAN (0));
/
CREATE TABLE LIVE_TBL
(
LIVE_TABLE_ID NUMBER,
CNT NUMBER
);
/
我将一些随机但有组织的数据插入到临时表中:
BEGIN
FOR i IN 1..100 -- Batch
LOOP
FOR j IN 1..10 -- Id
LOOP
INSERT INTO STG_TBL
SELECT j, ROUND(DBMS_RANDOM.VALUE*100,0) FROM DUAL, i;
END LOOP;
END LOOP;
END;
/
然后我尝试执行合并但语法显示它不正确...指示源表的select语句有效但不在合并中。除了使用Execute Immediate Syntax之外,还有什么想法让它有效?
BEGIN
FOR i IN 1..100 -- Batch
LOOP
MERGE INTO LIVE_TBL tgt
USING(SELECT LIVE_TBL_ID, CNT FROM STG_TBL PARTITION FOR (i)) src
ON (tgt.LIVE_TBL_ID = src.LIVE_TBL_ID)
WHEN MATCHED
THEN UPDATE
SET tgt.CNT = tgt.CNT + src.CNT
WHEN NOT MATCHED
THEN INSERT (
tgt.LIVE_TBL_ID,
tgt.CNT
) VALUES (
src.LIVE_TBL_ID,
src.CNT
);
COMMIT;
EXECUTE IMMEDIATE 'ALTER TABLE DROP PARTITION FOR (' || i || ')';
END LOOP;
END;
/