문제

I have a stored procedure that has a parameter that can be either NULL or non-NULL value. I need to compare this parameter in the WHERE clause. If the parameter is non-NULL,

where ...
and parameter = non-NULL-value

will work.

But when the parameter will be null, it will not be ANSI-compliant:

where ...
and parameter = NULL

I don't want to write two separate queries. How do I ensure ANSI-compliance in the same query?

도움이 되었습니까?

해결책

Assuming that the value is passed to your query in a @value parameter, you can do it like this:

SELECT *
FROM MyTable
WHERE (@value IS NOT NULL AND parameter = @value) -- Or simply parameter = @value
   OR (@value IS NULL AND parameter IS NULL)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top