Azure synapse Merger syntax error for Merge into TSQL
Asked Answered
C

2

5

Just would like to know what's the problem of the following merge .
enter image description here

enter image description here

thanks

Classis answered 8/12, 2020 at 7:1 Comment(0)
N
5

As at today, MERGE is supported in Azure Synapse Analytics dedicated SQL Pools and in preview. Simply terminate your statement with a semi-colon, eg

MERGE INTO t1 USING t2
ON t1.col1 = t2.col1
WHEN MATCHED 
    THEN UPDATE SET t1.col2 = t2.col2
    
WHEN NOT MATCHED 
    THEN INSERT ( col1, col2 )
    VALUES ( col1, col2 );

Also ensure your target tables are HASH distributed in order to avoid the following error:

Msg 100087, Level 16, State 1, Line 41 Merge statements with a WHEN NOT MATCHED [BY TARGET] clause must target a hash distributed table.

See here for a full repro script. See the announcement here for more details:

https://azure.microsoft.com/en-gb/updates/new-merge-command-for-azure-synapse-analytics/

Newly answered 21/12, 2020 at 22:49 Comment(0)
L
1

Faced a similar issue recently. The MERGE code was correct. All I did was recreate the Target(destination) table along with Hash distribution on primarykey column.

Something like this:

CREATE TABLE [SchemaName].[TableName]
( 
    [Id] [int]  NOT NULL,
    [Column1] [nvarchar](50)  NOT NULL,
    [Column2] [datetime2](7)  NOT NULL,
    [Column3] [bit]  NOT NULL
)

WITH
(
    DISTRIBUTION = HASH ( [Id] ),  --(hash distributed table on this Id column)
    CLUSTERED COLUMNSTORE INDEX
)

The Merge code worked.

Littman answered 2/6, 2022 at 21:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.