CREATE PROCEDURE sp_UpdateHistoricPrices (
@deltaLakeTable VARCHAR(255),
@historicTable VARCHAR(255)
)
AS
BEGIN
-- Define temporary tables for staging new data
DECLARE @newPrices TABLE (
date DATE NOT NULL,
symbol VARCHAR(10) NOT NULL,
closing_price DECIMAL(5,2) NOT NULL
);
-- Read new data from Delta Lake table using external table
CREATE EXTERNAL TABLE #newPrices (
date DATE,
symbol VARCHAR(10),
closing_price DECIMAL(5,2)
)
WITH (
LOCATION = @deltaLakeTable,
FORMAT = 'DELTA'
);
INSERT INTO @newPrices
SELECT date, symbol, closing_price
FROM #newPrices;
-- Drop temporary external table
DROP EXTERNAL TABLE #newPrices;
-- Identify new data that's not in the historic table
INSERT INTO @historicTable (date, symbol, closing_price)
SELECT np.date, np.symbol, np.closing_price
FROM @newPrices np
LEFT JOIN @historicTable hp
ON np.date = hp.date AND np.symbol = hp.symbol
WHERE hp.date IS NULL;
-- Alternatively, you can update existing records if needed
-- UPDATE @historicTable
-- SET closing_price = np.closing_price
-- FROM @newPrices np
-- INNER JOIN @historicTable hp
-- ON np.date = hp.date AND np.symbol = hp.symbol;
-- Log the number of new records inserted
PRINT 'Inserted ' + CAST(@@ROWCOUNT AS VARCHAR(10)) + ' new price records.';
END
GO
Explanation:
@deltaLakeTable:
The path to the Delta Lake table containing new price data.@historicTable:
The name of the existing historic price table in Synapse Analytics.@newPrices
to store the read data from the Delta Lake table.#newPrices
pointing to the Delta Lake location and format. Then, we insert the data into the temporary table @newPrices
.@newPrices
and @historicTable
. If a date-symbol combination doesn't exist in the historic table, it's considered new and inserted into the historic table.Note:
date
, symbol
, and closing_price
columns. Adapt the column names and table structures if yours differ.This provides a basic framework for comparing and inserting new data from a Delta Lake table into a historic price table using T-SQL stored procedures in Azure Synapse Analytics. Adapt and extend it based on your specific needs and data schema.