您可以尝试以下 SQL。它从左外连接计算代码 <> 'H' 和 tsDate = null 的数量。如果计数 > 0,则返回 false。
DECLARE @Holiday TABLE
(
hDate DATETIME NOT NULL
)
DECLARE @TimeSheet TABLE
(
tsDate DATETIME,
Code NCHAR(1)
)
INSERT INTO @Holiday (hDate)
VALUES ('2012-07-04'), ('2012-12-24'), ('2012-12-25'), ('2012-12-26')
INSERT INTO @TimeSheet (tsDate, Code)
VALUES ('2012-12-27', 'W'), ('2012-12-24', 'H'), ('2012-12-25', 'W')
DECLARE @StartDate DATETIME = '2012-12-24'
DECLARE @EndDate DATETIME = '2012-12-30'
SELECT
CASE WHEN COUNT(1) = 0 THEN 'TRUE' ELSE 'FALSE' END AS 'BooleanResult'
FROM
(
SELECT hDate
FROM @Holiday
WHERE hDate BETWEEN @StartDate AND @EndDate
) H
LEFT JOIN @TimeSheet TS
ON TS.tsDate = H.hDate
WHERE
TS.Code != 'H' OR TS.tsDate IS NULL and 'H' not in (select doecode from timesheet
where timesheet.tsdate = TS.tsDate