0

当有嵌套查询时,我无法确定参考范围。例如,我似乎无法在我的内部连接中引用 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
4

1 回答 1

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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Both of these appear to be referencing CU in a way that could potentially cause the error you are reporting.

于 2012-05-05T05:19:18.843 回答