当存在嵌套查询时,我无法确定引用范围。例如,我似乎无法在我的内连接中引用NoCarsFound.CU。我不明白为什么在我的连接中我不能引用以前的结果集...我想我不会在我的内连接的ON比较中引用先前的选择结果或引用别名的范围。
我也得到不明确的列名'CU'
所以我不断收到错误,说它不知道NoCarsFound.CU。我甚至尝试引用直接视图,例如vwInvalidCars.CU,它也不了解vwInvalidCars。
create procedure [Rac].[GetCarStatDetails_sp]
@Year int,
@CarTitle varchar(30),
@Company varchar(31),
@CU varchar(12),
@UserName varchar(50)
as
BEGIN
DECLARE @CarMatch table
(
FaceValueTol varchar(100),
FaceValueDesc varchar(100),
Year int,
CU varchar(16),
PaintTypeDesc varchar(50)
)
insert into @CarMatch
Select temp1.FaceValueTol,
temp1.FaceValueDesc,
temp1.Year,
temp1.CU,
temp1.PaintTypeDesc
from Rac.viewCarBase as temp1
join (select Username, userCars.CU from Nep.viewUserCars userCars where UserName = @UserName) as userCars on userCars.CU = temp1.CU
INNER JOIN
(
Select CU,
from Rac.vwCarFactor carfactors
where RiskFactorTypeID not in (334,553,34334,534,7756)
Group by CU
) as temp
on
and temp1.CU = temp.CU
and temp1.PaintTypeDesc = temp.CalcPaintTypeDesc
Where
temp1.RiskFactorTypeID=4
and temp1.[Year]=@Year
and (temp1.CarTitle=@CarTitle or @CarTitle='<All>')
and (temp1.CU=@CU or @CU='<All>')
SELECT ProductID_bo,
Coalesce(CarTitle_bo,LTRIM(RTRIM(CarTitle))) as CarTitle,
Coalesce(Company_bo,LTRIM(RTRIM(Company))) as Company,
Coalesce(CU_bo,LTRIM(RTRIM(CU))) as CU
FROM
Rac.viewCarBase as NoCarsFound
join (select Username, userCars.CU from Nep.viewUserCars userCars where UserName = @UserName) as userCars on userCars.CU = NoCarsFound.CU
LEFT OUTER JOIN
(
Select ProductID_bo,
CarTitle_bo,
Company_bo,
CU_bo,
from (
SELECT ProductID as ProductID_bo,
LTRIM(RTRIM(CarTitle)) as CarTitle_bo,
LTRIM(RTRIM(Company)) as Company_bo,
FROM Rac.viewCarBase
join (select Username, userCars.CU from Nep.viewUserCars userCars where UserName = @UserName) as userCars on userCars.CU = Rac.viewCarBase.CU
where ProductID in (Select ProductID from @CarMatch) and
and (CarTitle=@CarTitle or @CarTitle='<All>')
and (Company=@Company or @Company='<All>')
and (CU=@CU or @CU='<All>')
) AS SUB1
Group By
CarTitle_bo,
Company_bo,
CU_bo,
ON
NoCarsFound.CU = CarsFoundDeals.CU_bo
where
and (CarTitle=@CarTitle or @CarTitle='<All>')
and (Company=@Company or @Company='<All>')
and (CU=@CU or @CU='<All>')
end
答案 0 :(得分:2)
我将从上面的内容中查看以下两个SQL:
LEFT OUTER JOIN
(
Select ProductID_bo,
CarTitle_bo,
Company_bo,
CU_bo,
from (
SELECT ProductID as ProductID_bo,
LTRIM(RTRIM(CarTitle)) as CarTitle_bo,
LTRIM(RTRIM(Company)) as Company_bo,
FROM Rac.viewCarBase
join (select Username, userCars.CU from Nep.viewUserCars userCars where UserName = @UserName) as userCars on userCars.CU = Rac.viewCarBase.CU
where ProductID in (Select ProductID from @CarMatch) and
and (CarTitle=@CarTitle or @CarTitle='<All>')
and (Company=@Company or @Company='<All>')
and (CU=@CU or @CU='<All>') --<--HERE!!!!!!!!!!!!!!!!!!!!!!!!
) AS SUB1
和
where
and (CarTitle=@CarTitle or @CarTitle='<All>')
and (Company=@Company or @Company='<All>')
and (CU=@CU or @CU='<All>') -- <--HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
这两个似乎都引用了CU
,可能会导致您报告的错误。