将NULL与SQL Server中的字符串进行比较

时间:2014-09-29 07:12:34

标签: sql sql-server tsql

MERGE  DestinationTable AS D 
USING  SourceTable AS S
ON D.Alternate_ID = S.ID
WHEN MATCHED AND
(
      D.ID <> S.ID
   OR D.col1 <> S.col1
   OR D.col2 <> S.col2
   OR D.col3 <> S.col3
   OR D.col4 <> S.col4
   OR D.col5 <> S.col5
   OR D.col6 <> S.col6
   OR D.col7 <> S.col7
   OR D.col8 <> S.col8
)

大家好,如果使用上面的Merge语句片段更改了SourceTable中的任何列值,我会尝试更新DestinationTable。

但是,如果我在目标列中有NULL值并且源中有字符串或位值,则比较D.col8 <> S.col8将返回false,因为SQL处理与NULL值的比较方式。因此,DestinationTable不会使用SourceTable中的新值进行更新。

处理此问题的更好方法是什么。如果D.Col8为NULL且S.Col8具有字符串或位值,我仍然希望为D.col8 <> S.col8

这样的表达式返回true

如果我在S.Col8中有一个“Test”值而在D.Col8中有NULL,我想将Destination列从NULL更新为“Test”

5 个答案:

答案 0 :(得分:3)

MERGE  DestinationTable AS D 
USING  SourceTable AS S
ON D.Alternate_ID = S.ID
WHEN MATCHED AND
(
      D.ID <> S.ID
   OR (D.col1 IS NULL AND S.col1 IS NOT NULL) 
      OR (D.col1 IS NOT NULL AND S.col1 IS NULL) 
      OR D.col1 <> S.col1
   OR (D.col2 IS NULL AND S.col2 IS NOT NULL) 
      OR (D.col2 IS NOT NULL AND S.col2 IS NULL) 
      OR D.col2 <> S.col2 
   OR (D.col3 IS NULL AND S.col3 IS NOT NULL) 
      OR (D.col3 IS NOT NULL AND S.col3 IS NULL) 
      OR D.col3 <> S.col3 
   OR (D.col4 IS NULL AND S.col4 IS NOT NULL) 
      OR (D.col4 IS NOT NULL AND S.col4 IS NULL) 
      OR D.col4 <> S.col4 
   OR (D.col5 IS NULL AND S.col5 IS NOT NULL) 
      OR (D.col5 IS NOT NULL AND S.col5 IS NULL) 
      OR D.col5 <> S.col5 
   OR (D.col6 IS NULL AND S.col6 IS NOT NULL) 
      OR (D.col6 IS NOT NULL AND S.col6 IS NULL) 
      OR D.col6 <> S.col6 
   OR (D.col7 IS NULL AND S.col7 IS NOT NULL) 
      OR (D.col7 IS NOT NULL AND S.col7 IS NULL) 
      OR D.col7 <> S.col7 
   OR (D.col8 IS NULL AND S.col8 IS NOT NULL) 
      OR (D.col8 IS NOT NULL AND S.col8 IS NULL) 
      OR D.col8 <> S.col8 
)

答案 1 :(得分:1)

使用BINARY_CHECKSUM

,你可以做得更简单
MERGE  DestinationTable AS D 
USING  SourceTable AS S
ON D.Alternate_ID = S.ID
WHEN MATCHED AND 
 BINARY_CHECKSUM(d.col1, d.col2, ...,d.col8) <> BINARY_CHECKSUM(s.col1, ..., s.col8)

有一个小的碰撞概率有假阴性(值改变,但校验和是相同的),但可以忽略不计。

答案 2 :(得分:0)

试试这个:

...
WHEN MATCHED AND
(
   ISNULL(D.ID, '') <> S.ID
   OR ISNULL(D.col1, '') <> S.col1
   OR ISNULL(D.col2, '') <> S.col2
   OR ISNULL(D.col3, '') <> S.col3
   OR ISNULL(D.col4, '') <> S.col4
   OR ISNULL(D.col5, '') <> S.col5
   OR ISNULL(D.col6, '') <> S.col6
   OR ISNULL(D.col7, '') <> S.col7
   OR ISNULL(D.col8, '') <> S.col8
)

答案 3 :(得分:0)

尝试类似

的内容
MERGE  DestinationTable AS D 
USING  SourceTable AS S ON D.Alternate_ID = S.ID
WHEN MATCHED AND
(
   ISNULL(D.ID, '') <> ISNULL(S.ID,'')
   OR ISNULL(D.col1, '') <> ISNULL(S.col1,'')
   OR ISNULL(D.col2, '') <> ISNULL(S.col2,'')
   OR ISNULL(D.col3, '') <> ISNULL(S.col3,'')
   OR ISNULL(D.col4, '') <> ISNULL(S.col4,'')
   OR ISNULL(D.col5, '') <> ISNULL(S.col5,'')
   OR ISNULL(D.col6, '') <> ISNULL(S.col6,'')
   OR ISNULL(D.col7, '') <> ISNULL(S.col7,'')
   OR ISNULL(D.col8, '') <> ISNULL(S.col8,'')
)

D.IDS.IDD.colxS.colx也可能是Null

答案 4 :(得分:0)

INTERSECT and EXCEPT以您希望的方式处理NULL。 (即NULL = NULL

...
WHEN MATCHED 
     AND EXISTS (SELECT D.Col1, D.Col2, ...
                 EXCEPT S.Col1, S.Col2, ...)