-4

设想:

是源表SourceData (名称、编号、日期)

是否存在表ProdData (ProdName、ProdNumber、CreatedDate)

要求:

如果产品数据中已经存在,请勿从源导入!!!

将行从源导入到产品数据,保留现有行,追加新行,映射列,例如:

  • 名称 -> 产品名称
  • 编号 -> 产品编号
  • 日期 -> CreatedDate(如果 Date NULL 添加 SystemDate 时间

输出数据示例:

源数据

Name | Number | Date
-----+--------+------ 
A    | 1      | 2012
B    | 2      | NULL

产品数据

ProdName | ProdNumber | CreatedDate
---------+------------+------------
Existing |    123     | 2018
A        |    1       | 2012    
B        |    2       | 2020
4

1 回答 1

0

这样的东西会满足您的需求吗?

INSERT ProdData (ProdName, ProdNumber, CreatedDate)
SELECT 
    SourceData.name as "ProdName",
    SourceData.Number as "ProdNumber",
    NVL(SourceData.Date,to_date(sysdate,'YYYY') as "CreatedDate"
FROM 
    SourceData
WHERE 
    NOT EXISTS (SELECT
                    1 
                FROM 
                    ProdData PD2 
                WHERE 
                    PD2.ProdName = SourceData.name
                    and PD2.ProdNumber = SourceData.Number
                    and NVL(PD2.CreatedDate,to_date(sysdate,'YYYY') = nvl(SourceData.Date,to_date(sysdate,'YYYY');

另一个替代选项可能是这样的:

CREATE TABLE ProdData_new
WITH
    (
      DISTRIBUTION = HASH(ProdName)
    , CLUSTERED INDEX (ProdNumber)
    )
AS
SELECT 
      pdold.ProdName AS ProdName
    , pdold.ProdNumber AS ProdNumber
    , NVL(pdold.CreatedDate,to_date(sysdate,'YYYY')) AS CreatedDate
FROM      
    ProdData AS pdold

UNION 

SELECT 
      sdold.name AS ProdName
    , sdold.Number AS ProdNumber
    , NVL(sdold.Date,to_date(sysdate,'YYYY') AS CreatedDate
FROM 
    SourceData AS sdold
;

--Optional Renaming of old tables
--RENAME OBJECT ProdData TO ProdData_old;
--RENAME OBJECT SourceData TO SourceData_old;
--RENAME OBJECT ProdData_new TO ProdData;
--DROP TABLE ProdData_old;
--DROP TABLE SourceData_old;

或者可能更像是:

CREATE TABLE ProdData_new
    ( ProdName NVARCHAR NOT NULL 
    , ProdNumber INT NOT NULL
    , CreatedDate INT NOT NULL)
WITH
    ( DISTRIBUTION = HASH(ProdName)
    , CLUSTERED INDEX (ProdNumber) )
AS
SELECT 
      pdold.ProdName AS ProdName
    , pdold.ProdNumber AS ProdNumber
    , NVL(pdold.CreatedDate,to_date(sysdate,'YYYY')) AS CreatedDate
FROM      
    ProdData AS pdold

UNION 

SELECT 
      sdold.name AS ProdName
    , sdold.Number AS ProdNumber
    , NVL(sdold.Date,to_date(sysdate,'YYYY') AS CreatedDate
FROM 
    SourceData AS sdold
;

--Optional Renaming of old tables
--RENAME OBJECT ProdData TO ProdData_old;
--RENAME OBJECT SourceData TO SourceData_old;
--RENAME OBJECT ProdData_new TO ProdData;
--DROP TABLE ProdData_old;
--DROP TABLE SourceData_old;

在不知道您尝试过什么、您的特定语法和您的 DB_Schema_Table 设置的情况下,这一切都有些猜测。

于 2020-04-02T21:39:03.997 回答