protected virtual string MergeFromStagingSql(bool doNotUpdateIfExists = false) { var pivotColumns = PropertiesForPivotSet.SelectMany(p => p.Select(i => i.GetColumnName(base.StoreObject))).ToHashSet(); string whenNotMatchedStatement = $@"WHEN NOT MATCHED BY TARGET THEN INSERT ({string.Join(", ", PropertiesToInsert.Select(i => $"[{i.GetColumnName(base.StoreObject)}]"))}) VALUES ({string.Join(", ", PropertiesToInsert.Select(i => $"S.[{i.GetColumnName(base.StoreObject)}]"))})"; string whenMatchedStatement = this.GetWhenMatchedMergeStatement(pivotColumns, doNotUpdateIfExists); string pivotCriteria = this.BuildPivotCriteria(); return($@"MERGE {SqlTargetTable} AS T USING {SqlStagingTableName} AS S ON {pivotCriteria} {whenNotMatchedStatement} {whenMatchedStatement} OUTPUT INSERTED.*, S.[{TempColumnNumOrderName}], $action INTO {SqlOutputStagingTableName} option(recompile);"); }
private string BuildPivotCriteria() { return(string.Join(" OR ", PropertiesForPivotSet.Select(this.BuildUnitPivotCriteria))); }