another twist to the UPDATE-INSERT process is this...
consider a given as below:
now,
i would like that for every NEW record in the source table, they would be ADDED to the destination table.
AND, for any record that is updated/changed in the source table, it too would be ADDED to the destination table.
without the benefit of oracle's MINUS operator, this is how i implemented it...
would the above be an efficient way of implementing this considering tables with dozens of fields and thousands of records? or is there any other way, if there are, i would really appreciate any comment.
thanks!
consider a given as below:
Code:
* T_Source_Table (Record_ID, Record_Name, Record_Value)
* T_Destination_Table (Product_ID, Product_Code, Product_Name, Product_Value)
* T_Source_Table.Record_ID maps to T_Destination_Table.Product_Code
* T_Destination_Table.Product_ID is an automatically generated int (identity)
now,
i would like that for every NEW record in the source table, they would be ADDED to the destination table.
AND, for any record that is updated/changed in the source table, it too would be ADDED to the destination table.
without the benefit of oracle's MINUS operator, this is how i implemented it...
Code:
CREATE PROCEDURE prc_TestUpdateInsert_History
AS
-- insert process ALL new
INSERT INTO T_Destination_Table (
Product_Code,
Product_Name,
Product_Value
)
SELECT
Src.Record_ID,
Src.Record_Name,
Src.Record_Value
FROM
T_Source_Table AS Src LEFT JOIN T_Destination_Table
ON Src.Record_ID = T_Destination_Table.Product_Code
WHERE
T_Destination_Table.Product_ID IS NULL
-- create UNION table to find the UPDATED record
SELECT
Product_Code,
Product_Name,
Product_Value
INTO #tmpTableUnion
FROM
T_Destination_Table
WHERE
Product_ID IN (SELECT MAX(Product_ID)
FROM T_Destination_Table
GROUP BY Product_Code)
UNION
SELECT
Record_ID AS Product_Code,
Record_Name AS Product_Name,
Record_Value AS Product_Value
FROM
T_Source_Table
-- insert UPDATED values
INSERT INTO T_Destination_Table (
Product_Code,
Product_Name,
Product_Value
)
SELECT
Record_ID,
Record_Name,
Record_Value
FROM
T_Source_Table
WHERE
Record_ID IN (
-- find the CHANGED RECORD
SELECT Product_Code AS Record_ID
FROM #tmpTableUnion
GROUP BY Product_Code HAVING COUNT(*)>1
)
GO
would the above be an efficient way of implementing this considering tables with dozens of fields and thousands of records? or is there any other way, if there are, i would really appreciate any comment.
thanks!