正在从暂存 table 将数据加载到临时 table

Loading data to temp table from staging table

我已将 CSV 文件加载到暂存区。现在我想将此文件转移到临时 table。为此,我创建了 3 种不同的模式,即 STG(用于临时区域)、TMP(用于临时区域)和 TGT(用于目标区域或数据仓库)。这是在 STG 架构上暂存 table。

GRANT USAGE ON SCHEMA PRASHANT_DWH.STG TO ROLE ACCOUNTADMIN;
CREATE OR REPLACE TABLE PRASHANT_DWH_STG_COUNTRY (
    ID NUMBER(38,0) NOT NULL,
    COUNTRY_DESC VARCHAR(256)
    );

这是 TMP 架构上的临时 table。我添加了更多约束,例如维护列和代理键。

GRANT USAGE ON SCHEMA PRASHANT_DWH.TMP TO ROLE ACCOUNTADMIN;
CREATE OR REPLACE TABLE PRASHANT_DWH_TMP_COUNTRY (
    ID NUMBER(38,0) NOT NULL,
    COUNTRY_DESC VARCHAR(256),
    RCD_INS_TS TIMESTAMP, -- aaja ko date
    RCD_UPD_TS TIMESTAMP, -- aaja ko date, if updated then only updated date
    RCD_START_DI DATE, -- system date 
    RCD_CLOSE_DI DATE, -- future date  
    RCD_CLOSE_FLG VARCHAR(1),
    ID_SUR_KEY NUMBER NOT NULL, -- autogenerated, else key + 1, 
    PRIMARY KEY (ID_SUR_KEY) 
    );

并且我尝试合并这两个 table 并插入维护列代理键。但是一直显示错误

   MERGE TMP.PRASHANT_DWH_TMP_COUNTRY AS TC
   USING STG.PRASHANT_DWH_STG_COUNTRY AS SC
   ON PDTC.ID = PDSC.ID 
   WHEN MATCHED THEN 
        UPDATE PDTC.ID_SUR_KEY = PDSC.ID + 1 
        INSERT (ID, COUNTRY_DESC,RCD_INS_TS,RCD_UPD_TS,RCD_START_DI,RCD_CLOSE_DI,RCD_CLOSE_FLG,ID_SUR_KEY) 
        VALUES (SC.ID,SC.COUNTRY_DESC,current_timestamp,current_timestamp,current_date,current_date,N,SC.ID + 1)
   WHEN NOT MATCHEN BY TARGET THEN 
        DELETE

给出错误:

SQL compilation error: 
  syntax error line 1 at position 6 unexpected 'TMP'. 
  syntax error line 5 at position 15 unexpected 'PDTC'. 
  syntax error line 6 at position 8 unexpected 'INSERT'. 
  syntax error line 6 at position 32 unexpected ','. 
  syntax error line 6 at position 43 unexpected ','. 
  syntax error line 6 at position 54 unexpected ','. 
  syntax error line 6 at position 67 unexpected ','. 
  syntax error line 6 at position 80 unexpected ','. 
  syntax error line 6 at position 94 unexpected ','. 
  syntax error line 6 at position 105 unexpected ')'. 
  syntax error line 7 at position 37 unexpected ','. 
  syntax error line 7 at position 55 unexpected ','. 
  syntax error line 7 at position 73 unexpected ','. 
  syntax error line 7 at position 86 unexpected ','. 
  syntax error line 7 at position 99 unexpected ','. 
  syntax error line 7 at position 101 unexpected ','. 
  syntax error line 7 at position 108 unexpected '+'. 
  syntax error line 7 at position 111 unexpected ')'. 

最终合并缺少一些东西。

  • MERGE
  • 之后的INTO
  • 我还完全限定了 table 名称(可能不需要)
  • 你使用别名 PDTC & PDSC 他们是 TC & SC
  • RCD_CLOSE_FLG中插入的N是一个字符,需要用单引号括起来'N'
MERGE INTO PRASHANT_DWH.TMP.PRASHANT_DWH_TMP_COUNTRY AS TC
USING PRASHANT_DWH.STG.PRASHANT_DWH_STG_COUNTRY AS SC
    ON TC.ID = SC.ID 
WHEN MATCHED THEN 
    UPDATE TC.ID_SUR_KEY = SC.ID + 1
    INSERT (ID, COUNTRY_DESC, RCD_INS_TS, RCD_UPD_TS, RCD_START_DI, RCD_CLOSE_DI, RCD_CLOSE_FLG, ID_SUR_KEY) 
    VALUES (SC.ID, SC.COUNTRY_DESC, current_timestamp, current_timestamp, current_date, current_date, 'N', SC.ID + 1)
WHEN NOT MATCHEN BY TARGET THEN DELETE

但是你的 MERGE SQL 的尾巴有点乱,我不确定如何修复它,因为我不清楚你正在尝试做什么。

INSERT 应绑定到 WHEN MATCHED THENWHEN NOT MATCHED THEN 而你的 WHEN NOT MATCHEN BY TARGET THEN DELETE 似乎..

它应该是 WHEN NOT MATCHEN THEN DELETE,但这似乎会干扰 INSERT。

更新和插入最有意义,忽略删除路径,

MERGE INTO PRASHANT_DWH.TMP.PRASHANT_DWH_TMP_COUNTRY AS TC
USING PRASHANT_DWH.STG.PRASHANT_DWH_STG_COUNTRY AS SC
    ON TC.ID = SC.ID 
WHEN MATCHED THEN 
    UPDATE TC.ID_SUR_KEY = SC.ID + 1
WHEN NOT MATCHED THEN
    INSERT (ID, COUNTRY_DESC, RCD_INS_TS, RCD_UPD_TS, 
        RCD_START_DI, RCD_CLOSE_DI, RCD_CLOSE_FLG, ID_SUR_KEY) 
    VALUES (SC.ID, SC.COUNTRY_DESC, current_timestamp, 
        current_timestamp, current_date, current_date, 'N', 
        SC.ID + 1);