Error when running script

Here is the code

##############################################
$SQLMerge = "MERGE $SQLDatabase.dbo.$SQLTable Target
USING $SQLTempDatabase.dbo.$SQLTempTable Source
ON (Target.key_id = Source.key_id)
WHEN MATCHED 
     THEN UPDATE
     SET    Target.value_deleted_at = Source.value_deleted_at,
            Target.value_storage_quota = Source.value_storage_quota,
            Target.value_lti_context_id = Source.value_lti_context_id,
            Target.value_created_at = Source.value_created_at,
            Target.value_updated_at = Source.value_updated_at,
            Target.value_workflow_state = Source.value_workflow_state,
            Target.value_sortable_name = Source.value_sortable_name,
            Target.value_avatar_image_url = Source.value_avatar_image_url,
            Target.value_avatar_image_source = Source.value_avatar_image_source,
            Target.value_avatar_image_updated_at = Source.value_avatar_image_updated_at,
            Target.value_short_name = Source.value_short_name,
            Target.value_last_logged_out = Source.value_last_logged_out,
            Target.value_pronouns = Source.value_pronouns,
            Target.value_merged_into_user_id = Source.value_merged_into_user_id,
            Target.value_locale = Source.value_locale,
            Target.value_name = Source.value_name,
            Target.value_default_time_zone = Source.value_default_time_zone,
            Target.value_uuid = Source.value_uuid,
            Target.meta_ts = Source.meta_ts

 WHEN NOT MATCHED BY TARGET
 THEN INSERT (key_id,value_deleted_at,value_storage_quota,value_lti_context_id,value_created_at,value_updated_at,value_workflow_state,value_sortable_name,value_avatar_image_url,value_avatar_image_source,value_avatar_image_updated_at,value_short_name,value_last_logged_out,value_course_template_id,value_created_at,value_updated_at,value_pronouns,value_merged_into_user_id,value_locale,value_name,value_default_time_zone,value_uuid,meta_ts)
      VALUES (Source.key_id,Source.value_deleted_at,Source.value_storage_quota,Source.value_lti_context_id,Source.value_created_at,Source.value_updated_at,Source.value_workflow_state,Source.value_sortable_name,Source.value_avatar_image_url,Source.value_avatar_image_source,Source.value_avatar_image_updated_at,Source.value_short_name,Source.value_last_logged_out,Source.value_pronouns,Source.value_created_at,Source.value_updated_at,Source.value_pronouns,Source.value_merged_into_user_id,Source.value_locale,Source.value_name,Source.value_default_time_zone,Source.value_uuid,Source.meta_ts);"
# Running the INSERT Query
Invoke-SQLCmd -Query $SQLMerge -ServerInstance $SQLInstance -Username $SQLUsername -Password $SQLPassword 

This is the error message

Invoke-SQLCmd : Incorrect syntax near '.'. 
 Msg 102, Level 15, State 1, Procedure , Line 1.
At line:30 char:1
+ Invoke-SQLCmd -Query $SQLMerge -ServerInstance $SQLInstance -Username ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
    + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

william,
Welcome to the forum. :wave:t4:

This might be more of an SQL than a PowerShell issue. Have you tried asking this question in an SQL related forum?

Hi, welcome to the forum :wave:

Try specifying the query as a here-string

$SQLMerge = @"
MERGE $SQLDatabase.dbo.$SQLTable Target
USING $SQLTempDatabase.dbo.$SQLTempTable Source
ON (Target.key_id = Source.key_id)
WHEN MATCHED 

8< snipped for brevity >8
"@