Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Custom Function Syntax error Unexpected keyword MERGE

Below is the scd_utils.js code which is used for scd type 2 operation.

 

// includes/scd_utils.js
 
/**
* Generates SQL for SCD Type 2 operations using MERGE
*
*  {string} sourceTable - The source table or CTE
*  {string} targetTable - The target table
*  {Array<string>} keyColumns - List of key columns
*  {Array<string>} scdColumns - List of SCD columns
*  {string} loadDatetimeColumn - Column for load datetime
* @returns {string} SQL code for SCD Type 2 operations
*/
function scdType2(sourceTable, targetTable, keyColumns, scdColumns, loadDatetimeColumn) {
  const keyColumnsStr = keyColumns.join(", ");
  const keyColumnsMatch = keyColumns.map(col => `target.${col} = source.${col}`).join(" AND ");
  const scdColumnsStr = scdColumns.join(", ");
 
  return `
    MERGE INTO ${targetTable} AS target
    USING ${sourceTable} AS source
    ON (${keyColumnsMatch})
    WHEN MATCHED AND target.is_current = TRUE
        AND (target.${scdColumns.map(col => `target.${col} != source.${col}`).join(" OR ")})
    THEN
      UPDATE SET
        target.scd_valid_to = source.${loadDatetimeColumn},
        target.is_current = FALSE
    WHEN NOT MATCHED
    THEN
      INSERT (${keyColumnsStr}, ${scdColumnsStr}, scd_valid_from, scd_valid_to, is_current)
      VALUES (${keyColumnsStr}, ${scdColumnsStr}, source.${loadDatetimeColumn}, NULL, TRUE)
  `;
}
 
module.exports = { scdType2 }

 

The below .sqlx code calls the scd type 2 function :

 

-- Use the SCD Type 2 logic from the JavaScript file
${scd_utils.scdType2(
  `temp`,  // Source table (the CTE "temp" prepared above)
  `${self()}`,  // Target table (the table being defined here)
  ["id"],  // Key columns
  ["post_code", "suburb", "geometry"],  // SCD tracked columns
  "load_dt"  // Load datetime column
)}

 

Getting Compile Error : Syntax error: Unexpected keyword MERGE at [24:5]

0 1 427
1 REPLY 1