2017-04-22 59 views
0

我有這個存儲過程:內部連接在存儲過程

ALTER PROCEDURE [dbo].[Get_All_Items] 
AS 
BEGIN 
    SELECT 
     ItemID, ItemName, CategoryID, CountryID, 
     ItemSize, ColorID, ProductionDate, ExpiryDate, 
     UnitName, FirstUnitBarcode, FirstItemQuantity, 
     FirstUnitDefult, FirstUnitLimit, 
     UnitsTbl.SecondUnit, SecondUnitBarcode, SecondItemQuantity, 
     SecondUnitDefult, SecondUnitLimit, 
     ThirdUnit, ThirdUnitBarcode, ThirdItemQuantity, 
     ThirdUnitDefult, ThirdUnitLimit, UnitDefult, 
     ItemImage, ItemStatus, ItemsMainTbl.Nots, 
     ItemsMainTbl.CreatedBy, ItemsMainTbl.CreatedDate, 
     ItemsMainTbl.ModifiedBy, ItemsMainTbl.ModifiedDate 
    FROM 
     ItemsMainTbl 
    INNER JOIN 
     UnitsTbl ON ItemsMainTbl.FirstUnit = UnitsTbl.UnitID 
    INNER JOIN 
     UnitsTbl ON ItemsMainTbl.SecondUnit = UnitsTbl.UnitID 
END 

條件:

  • ItemsMainTbl.FirstUnit = UnitsTbl.UnitID
  • ItemsMainTbl.SecondUnit = UnitsTbl.UnitID
  • ItemsMainTbl .ThirdUnit = UnitsTbl.UnitID

如何連接該公共列上的所有三個表?

我只拿到了第一個

ItemsMainTbl.FirstUnit = UnitsTbl.UnitID 

感謝

+0

與VB.NET無關,與Visual Studio無關。今後請使用正確的標籤。 – Bugs

+0

塞勒姆,描述你的表中的字段。不可能理解'Iam Not Getting This UnitName' – Ingaz

回答

0

您需要爲您的UnitsTbl定義參考。就OOP而言,將這些引用視爲同一類的新對象。而且,每當查詢表時使用引用都是一個好習慣。

SELECT * 
FROM 
    ItemsMainTbl t1 
INNER JOIN 
    UnitsTbl u1 ON t1.FirstUnit = u1.UnitID 
INNER JOIN 
    UnitsTbl u2 ON t1.SecondUnit = u2.UnitID 
INNER JOIN 
    UnitsTbl u3 ON t1.ThirdUnit = u3.UnitID 
1

如果你想多次參加同一個表,你需要指定一個別名連接表的每個實例,就像這樣:

SELECT * 
FROM ItemsMainTbl 
INNER JOIN UnitsTbl u1 ON ItemsMainTbl.FirstUnit = u1.UnitID 
INNER JOIN UnitsTbl u2 ON ItemsMainTbl.SecondUnit = u2.UnitID 
INNER JOIN UnitsTbl u3 ON ItemsMainTbl.ThirdUnit = u3.UnitID 

然後在SELECT語句中,可以在加入實例之間區分,像這樣:

SELECT u1.Barcode, -- Barcode from FirstUnit 
     u2.Barcode, -- Barcode from SecondUnit 
     u3.Barcode -- Barcode from ThirdUnit 
+0

爲什麼'INNER JOIN'變成'LEFT OUTER'? – Ingaz

+0

糟糕!並不是要改變連接類型。發佈編輯。 –