COALESCE 메서드를 사용하면 열에 NULL 값이 있으면 NULL 검색 조건 (검색 조건 무시)이 많은 데이터베이스의 행을 반환하지 않는다는 점에서 문제가 있습니다.
CREATE TABLE dbo.Test_Coalesce (
my_id INT NOT NULL IDENTITY,
my_string VARCHAR(20) NULL)
GO
INSERT INTO dbo.Test_Coalesce (my_string) VALUES (NULL)
INSERT INTO dbo.Test_Coalesce (my_string) VALUES ('t')
INSERT INTO dbo.Test_Coalesce (my_string) VALUES ('x')
INSERT INTO dbo.Test_Coalesce (my_string) VALUES (NULL)
GO
DECLARE @my_string VARCHAR(20)
SET @my_string = NULL
SELECT * FROM dbo.Test_Coalesce WHERE my_string = COALESCE(@my_string, my_string)
GO
에만 때문에 열 my_string이 NULL 인 행이 점점 효과가 다시 두 행을 얻을 것이다 :
예를 들어, SQL Server에서 다음 코드 2000를 시도
my_string = COALESCE(@my_string, my_string) =>
my_string = COALESCE(NULL, my_string) =>
my_string = my_string =>
NULL = NULL
물론 NULL은 NULL과 같지 않습니다.
나는 고수하려고 :
SELECT
my_id,
my_string
FROM
dbo.Test_Coalesce
WHERE
(@my_string IS NULL OR my_string = @my_string)
는 물론, 당신이 당신이 원하는 와일드 카드 또는 어떤 다른 사람을 사용하여 조정할 수 있습니다.
는 내가 목표 테이블에 NULL 데이터 값을 많이 가지고 있기 때문에 케이드 루의 솔루션은 나에게 가장 일한 여기에 언급 싶었지만, 나는 모든 데이터가 있다면 유착이 어떻게 작동하는지 잘 볼 수있는 내 열, 그래서 BoltBait의 솔루션에 upvote 확실히 장점을 보증합니다. –
아론 버트 랜드 (Aaron Bertrand)는 이것을 "주방 싱크 절차"라고 부르며 http://sqlsentry.tv/the-kitchen-sink-procedure/ 및 http : // blogs에서 볼 수있는 이런 유형의 문제를 다루는 좋은 생각을 갖고 있습니다. .sqlsentry.com/aaronbertrand/backtobasics-updated-kitchen-sink-example /. – JamieSee