这是我的发言
IF (@UserName IS NULL AND @EditorKey IS NULL) OR (@UserName IS NOT NULL AND @EditorKey IS NOT NULL) BEGIN RAISERROR ('One of @UserName,@EditorKey must be non-null.',15,0) RETURN END
我想要的是能够做这样的事情:
IF (@UserName IS NOT NULL) XOR (@EditorKey IS NOT NULL) BEGIN RAISERROR ('One of @UserName,0) RETURN END
对于两个参数,它不是一个很大的交易,但有些过程有三四个,只有一个可以通过,其余的应该是空的.
解决方法
不是很简洁,但你可以扩展这样的逻辑:
WHERE (NOT ((@UserName IS NOT NULL) AND (@EditorKey IS NOT NULL))) AND ((@UserName IS NOT NULL) OR (@EditorKey IS NOT NULL))
或使用按位XOR运算符(^):
WHERE (CASE WHEN (@UserName IS NOT NULL) THEN 1 ELSE 0 END) ^ (CASE WHEN (@EditorKey IS NOT NULL) THEN 1 ELSE 0 END) = 1
您可以使用类似的方法,其中有三个或四个参数,只有一个必须有一个值:
WHERE (CASE WHEN (@Var1 IS NOT NULL) THEN 1 ELSE 0 END) + (CASE WHEN (@Var2 IS NOT NULL) THEN 1 ELSE 0 END) + (CASE WHEN (@Var3 IS NOT NULL) THEN 1 ELSE 0 END) + (CASE WHEN (@Var4 IS NOT NULL) THEN 1 ELSE 0 END) = 1