这样的东西会满足您的需求吗?
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 设置的情况下,这一切都有些猜测。