Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The correlation name 'MR' is specified multiple times in a FROM clause

Tags:

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
like image 391
Sambasiva Avatar asked Dec 18 '13 12:12

Sambasiva


3 Answers

You must use a different alias for each join of K_RT_MasterRetailStores.

like image 111
grahamj42 Avatar answered Nov 15 '22 07:11

grahamj42


The others are saying to use a different alias, but perhaps it's not clear to you what they mean:

 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

I note that you're not using anything from the second join in the SELECT list. Maybe you wanted to use that instead of 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
like image 32
Damien_The_Unbeliever Avatar answered Nov 15 '22 07:11

Damien_The_Unbeliever


You've joined K_RT_MasterRetailStores on twice with the same alias.

Depending on whether this join needs to be in twice you either need to

  • Delete the second join
  • Use a different alias
like image 38
Liath Avatar answered Nov 15 '22 07:11

Liath