有没有办法在不收到错误的情况下重写此查询?:子查询返回的值超过1。
这是查询用于表值函数中的LEFT JOIN。根据要求,我需要默认提取两个方案ID(如果参数值为NULL或为空)
DECLARE @pScenarioName AS VARCHAR(30)
select
externalID,
PropertyAssetId,
LeaseID,
BeginDate
from ae11.dbo.ivw_Leases
WHERE PropertyAssetID IN
(select ID from AE11.dbo.PropertyAssets where scenarioID IN
(CASE WHEN isnull(@pScenarioName, '') = ''
THEN (select top 2 ID from rvw_Scenarios where Name like '[0-9][0-9][0-9][0-9]%'
AND LEN(Name) = 8
order by Name desc)
ELSE
(select ID from aex.dbo.rvw_Scenarios
where [Name] IN (@pScenarioName))
END)
)
答案 0 :(得分:1)
我没有对此进行过测试,但在处理参数时我使用了类似的方法。当然,如果ID的顺序在您的第二个子查询中至关重要,这不一定有效。
SELECT ExternalID
,PropertyAssetId
,LeaseID
,BeginDate
FROM ae11.dbo.ivw_Leases
WHERE PropertyAssetID IN
(SELECT ID
FROM AE11.dbo.PropertyAssets
WHERE scenarioID IN
(SELECT TOP 2 ID
FROM rvw_Scenarios
WHERE (@ISNULL(@pScenarioName,'') = ''
AND Name LIKE '[0-9][0-9][0-9][0-9]%'
AND LEN(Name) = 8)
ORDER BY Name DESC
UNION ALL
SELECT ID FROM aex.dbo.rvw_Scenarios
WHERE (@pScenarioName IS NOT NULL)
AND [Name] IN (@pScenarioName)))