简体   繁体   English

SQL MERGE 语句不区分大小写

[英]SQL MERGE statement not case sensitive

I have the following MERGE statement:我有以下MERGE声明:

MERGE TargetTable t
USING SourceTable s ON (t.ID = s.ID)

WHEN MATCHED 
     AND EXISTS (SELECT s.Day, s.Date, s.Name
                 EXCEPT
                 SELECT t.Day, t.Date, t.Name)
    THEN UPDATE 
         SET t.Day = s.Day,
             t.Date = s.Date,
             t.Name = s.Name

WHEN NOT MATCHED BY TARGET
    THEN INSERT (ID, Day, Date, Name)
         VALUES (s.ID, s.Day, s.Date, s.Name);

There are more columns however I just shorten it for ease of reading.还有更多列,但我只是将其缩短以方便阅读。

So when I run this it works fine except: if I change the case of something in source table, it does not get updated in the target table.因此,当我运行它时,它工作正常,除了:如果我更改源表中某些内容的大小写,它不会在目标表中更新。 I believe this is solved by using COLLATE for UTF8 however cannot figure it out.我相信这可以通过对 UTF8 使用 COLLATE 来解决,但无法弄清楚。

EDIT: What I mean by if I change something in the source table is: For testing purposes I will execute the MERGE, then change a varchar field but only the case of a letter.编辑:如果我更改源表中的某些内容,我的意思是:出于测试目的,我将执行 MERGE,然后更改 varchar 字段,但仅更改字母的大小写。 Then when I run MERGE again that change does not get propagated to the target table as it does not see that as a change IE it is case insensitive然后,当我再次运行 MERGE 时,更改不会传播到目标表,因为它没有将其视为更改 IE,它不区分大小写

Thanks谢谢

Use a case-sensitive collation to qualify your columns.使用区分大小写的排序规则来限定您的列。

Suppose your varchar columns were defined with a case-insensitive collation SQL_Latin1_General_CP1_CI_AS (explicitly, or by way of default for the database).假设您的 varchar 列是使用不区分大小写的排序SQL_Latin1_General_CP1_CI_AS定义的(显式地,或者数据库的默认值)。 Your subquery with EXCEPT would become:您的 EXCEPT 子查询将变为:

SELECT s.Day, s.Date, s.Name COLLATE SQL_Latin1_General_CP1_CS_AS
EXCEPT
SELECT t.Day, t.Date, t.Name COLLATE SQL_Latin1_General_CP1_CS_AS

The CS in the collation's name stands for case-sensitive.排序规则名称中的CS代表区分大小写。 The CI for case-insensitive.不区分大小写的CI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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