SQL Server Null Logical XOR

Car*_*lin 6 sql t-sql sql-server constraints xor

我正在(Microsoft)Sql Server 2008表上创建约束.我有两列不同的数据类型.一列必须始终为null,但不能同时为两列(逻辑异或或异).我目前有一个工作表达.

(@a is null or @b is null) and not (@a is null and @b is null)
Run Code Online (Sandbox Code Playgroud)

我的问题是,编写此代码有更简洁的方法吗?

要测试它,您可以使用此代码...

declare @a int
declare @b varchar(5)

set @a=1
set @b='XXXXX'

if (@a is null or @b is null) and not (@a is null and @b is null)
  select 'pass'
else
  select 'fail'
Run Code Online (Sandbox Code Playgroud)

SQL*_*ace 5

我会比较喜欢

if (@a is null and @b is not null) or (@a is not null and @b is null)
Run Code Online (Sandbox Code Playgroud)

在我看来,它更清晰一些