这是我的有效查询:
Select AsOfDate, Family, Type, DocID, Title, Date1, Date2, Date3, Stat1, Stat2, Stat3
FROM DocumentationData
WHERE Type = @Type
AND Family = @Family
AND AsOfDate = (SELECT Max(AsOfDate)
FROM DocumentationData
WHERE AsOfDate <= @CurrentDate )
我想添加一个条件。如果 @ActiveOnly 为真,我想要额外的 Usage = 'Active" 约束。
这是我正在尝试的,但会导致错误:
Select AsOfDate, Family, Type, DocID, Title, Date1, Date2, Date3, Stat1, Stat2, Stat3
FROM DocumentationData
WHERE Type = @Type
AND Family = @Family
AND AsOfDate = (IF (@ActiveOnly = 'TRUE')
BEGIN
SELECT Max(AsOfDate)
FROM DocumentationData
WHERE AsOfDate <= @CurrentDate
AND Usage = 'Active'
END
ELSE
BEGIN
SELECT Max(AsOfDate)
FROM DocumentationData
WHERE AsOfDate <= @CurrentDate
END
)