define source by query
fmms opened this issue · comments
Felix Möller commented
Hi,
i am using the addition of #21. For my use case of simple ETL merges it would be even better if the source could be a SQL query.
@sourceStmt='SELECT a, b FROM Staging.table'
regards
Daniel Nolan commented
Investigated this but unfortunately it won't work given that datatypes and keys must be predefined in order for the proc to successfully generate a merge
statement.
If some combination of the @cols_to_include
, @cols_to_exclude
, @cols_to_join_on
and @from
parameters doesn't provide the flexibility or simplicity desired, then the temporary table approach can be used (albeit with the overhead of populating it via INTO
):
-- Put any query you like here, as long as it includes "INTO #TableName"
SELECT * INTO #CurrencyRateFiltered
FROM AdventureWorks.Sales.CurrencyRate
WHERE ToCurrencyCode = 'AUD';
-- Add a primary key to the temp table
ALTER TABLE #CurrencyRateFiltered ADD CONSTRAINT PK_Sales_CurrencyRate PRIMARY KEY CLUSTERED (CurrencyRateID);
-- Generate the MERGE statement with values
EXEC tempdb.dbo.sp_generate_merge
@table_name = '#CurrencyRateFiltered'
, @target_table = '[AdventureWorks].[Sales].[CurrencyRate]'
, @delete_if_not_matched = 0
, @include_use_db = 0;