在FROM子句中多次指定相关名称“MR”

时间:2013-12-18 12:48:12

标签: sql-server

Create PROCEDURE [dbo].[K_RT_FixedAsset]
@fromdate datetime,
@todate datetime
AS
BEGIN

SET NOCOUNT ON;

 select convert(varchar,FT.date,103)as date, MR.branch as frombranch,PD.productname as product,FT.vehicleno,FT.dcno,FT.tobranch 
from K_RT_FixedAssetTransfer FT
inner join K_RT_MasterRetailStores MR on MR.sno = FT.frombranch 
inner join K_RT_ProductDetails PD on PD.sno=FT.product
inner join K_RT_MasterRetailStores MR on MR.sno = FT.tobranch 
where FT.date between @fromdate and  @todate


END

3 个答案:

答案 0 :(得分:5)

您必须为K_RT_MasterRetailStores的每个联接使用不同的别名。

答案 1 :(得分:3)

其他人说要使用不同的别名,但也许你不清楚它们是什么意思:

 select convert(varchar,FT.date,103)as date, MR_from.branch as frombranch,
        PD.productname as product,FT.vehicleno,FT.dcno,FT.tobranch 
from K_RT_FixedAssetTransfer FT
inner join K_RT_MasterRetailStores MR_from --<-- First alias
           on MR_from.sno = FT.frombranch 
inner join K_RT_ProductDetails PD on PD.sno=FT.product
inner join K_RT_MasterRetailStores MR_to --<-- Second alias
           on MR_to.sno = FT.tobranch 
where FT.date between @fromdate and  @todate

我注意到您没有使用SELECT列表中第二个联接中的任何内容。也许您想使用它代替FT.tobranch

 select convert(varchar,FT.date,103)as date, MR_from.branch as frombranch,
        PD.productname as product,FT.vehicleno,FT.dcno,MR_to.branch 
from K_RT_FixedAssetTransfer FT
inner join K_RT_MasterRetailStores MR_from on MR_from.sno = FT.frombranch 
inner join K_RT_ProductDetails PD on PD.sno=FT.product
inner join K_RT_MasterRetailStores MR_to on MR_to.sno = FT.tobranch 
where FT.date between @fromdate and  @todate

答案 2 :(得分:1)

您已使用相同的别名加入K_RT_MasterRetailStores两次。

根据此连接是否需要两次,您需要

  • 删除第二个联接
  • 使用其他别名