這是否接近重現您所看到的問題?
DECLARE @StartDate datetime;
DECLARE @EndDate datetime;
-- Date range is the year 2000
SET @StartDate = '2000-01-01';
SET @EndDate = '2000-12-31';
DECLARE @TableA TABLE
(
col1 int NULL,
col2 int NULL
);
DECLARE @TableB TABLE
(
col1 int NULL,
col2 int NULL,
col3 datetime NULL
);
和:
INSERT @TableA
(col1, col2)
VALUES
(1, 1);
-- Row joins but date is not in year 2000
INSERT @TableB
(col1, col2, col3)
VALUES
(1, 1, '2001-07-01');
-- Test query (no rows)
SELECT
a.*,
b.*
FROM @TableA AS a
LEFT JOIN @TableB AS b
ON b.col1 = a.col1
WHERE
b.col1 IS NULL
OR
(
b.col1 = a.col1
AND b.col2 = a.col2
AND b.col3 BETWEEN @StartDate AND @EndDate
);
這是很容易爲外部的邏輯連接,ON
條款和WHERE
子句中NULL
-Extended行迅速失控。在使用OUTER JOIN
嘗試重寫它(推測是爲了獲得性能好處?)之前,我可能會重寫查詢,使用NOT EXISTS
和/或簡單的UNION ALL
以獲得正確的結果。
你不應該得到任何結果集。你應該得到一個錯誤,因爲語法是無效的(JOIN沒有ON)。請複製並粘貼運行的實際查詢。 –
另外,請提供樣本數據和期望的輸出。 – RedFilter
根據具體的數據庫,這可能會執行,但會導致笛卡爾聯接。 – kirbs