简体   繁体   中英

OPENJSON collation in Azure Synapse causes a collation conflict error

I have an OPENJSON command that takes the parsed JSON and LEFT joins it onto an existing table.

When I add the LEFT JOIN I get the error:

collation conflict between "Latin1_General_BIN2" and "SQL_Latin1_General_CP1_CI_AS"

The table has the same collation for all string columns as: SQL_Latin1_General_CP1_CI_AS

I've tried adding COLLATE DATABASE_DEFAULT in the LEFT JOIN, but with no improvement.

The query I'm using is roughly as:

DECLARE @json NVARCHAR(MAX) = '
{
    "ExampleJson": {
        "stuff": [
            {
                "_program_id": "hello",
                "work_date": "2021-03-23 00:00:00"
            }
        ]
    }
}';

SELECT *
FROM 
OPENJSON
(
    (
        @json
    ), '$.ExampleJson.stuff'
) 
 
WITH ( 
     [program_id] NVARCHAR(255) '$."program_id"'
     ,[work_date] DATETIME '$."work_date"'
) [json_data] 
  
 
LEFT JOIN 
    [existing_db_data]
    ON [existing_db_data].[program_id] = [json_data].[program_id] 

Interesting problem which I cannot reproduce but you should be able to resolve by placing the correct collation in either the WITH clause or after the join, eg

WITH ( 
     [program_id] NVARCHAR(255) COLLATE SQL_Latin1_General_CP1_CI_AS '$."_program_id"' 
     ,[work_date] DATETIME '$."work_date"'
) [json_data] 
    LEFT JOIN [existing_db_data] d ON d.[program_id] = [json_data].[program_id] COLLATE SQL_Latin1_General_CP1_CI_AS

Either should suffice, you do not need both.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM