簡體   English   中英

選擇要使用2個數據庫的qry

[英]Select qry to using 2 databases

我有以下查詢:

SELECT 
     --a.DateEntered,
     --a.InventoryId,
     a.SKU, a.QtyOnHand,
     b.Dateentered AS VDateEntered,
     b.GoLive, b.DateOnSite, b.CostPrice,
     --a.CurrentPrice,
     m.name AS Status,
     hrf.category, hrf.department, hrf.BrandedOB, hrf.Division,
     hrf.Fascia,
     (a.QtyOnHand * b.CostPrice) AS Cost_Value,
     NULL AS Item_Quantity, NULL AS Item,
     NULL AS Season, hrf.Company,
     (a.QtyOnHand * b.CurrentPrice) AS Sellilng_Value,
     b.merchandisingseason, b.CostPrice AS Costprice_RP,
     b.CurrentPrice, b.InventoryID,
     -- a.AverageUnitCost,
     -- a.AverageUnitCost AS RP_Stk_AverageUnitCost,
     -- a.CurrentPrice AS RP_Stk_Current_Price,
     -- a.Statusid,
     -- a.Quantity_Sign,
     (a.QtyOnHand * b.CostPrice) AS Cost_Value_RP,
     (a.QtyOnHand * b.AverageUnitCost) AS AWC_Value     
     -- a.StockReconciliationId,
     -- a.AverageUnitCost,
FROM
    [dbo].[FC03QTY] a
JOIN 
    dbo.inventory b ON a.SKU = b.SKU
LEFT JOIN
    (------Hierarchy-------
     SELECT 
         ih.InventoryId, hry.category, hry.department,
         hry.BrandedOB, hry.Division, hry.Fascia, hry.Company
     FROM
         (SELECT
              ihn.HierarchyNodeId, ihn.InventoryId
          FROM bm.InventoryHierarchyNode IHN
          GROUP BY ihn.HierarchyNodeId, ihn.InventoryId) IH
     JOIN 
         (SELECT 
              g.categoryid, g.category, h.department, i.BrandedOB,
              j.Division, K.Fascia, L.company
          FROM 
              Category g (NOLOCK)
          JOIN
              Department H ON g.departmentid = h.departmentID
          JOIN
              BrandedOB I (NOLOCK) ON h.BrandedOBID = i.BrandedOBID
          JOIN
              Division j (NOLOCK) ON i.divisionid = j.divisionid
          JOIN
              Fascia k (NOLOCK) ON j.fasciaid = k.fasciaID
          JOIN
              company l (NOLOCK) ON k.companyid = l.companyid
          GROUP BY
              g.categoryid, g.category, h.department,
              i.BrandedOB, j.Division, K.Fascia, L.company) HRY ON ih.HierarchyNodeId = hry.CategoryId
     GROUP BY
         ih.InventoryId, hry.category, hry.department,
         hry.BrandedOB, hry.Division, hry.Fascia, hry.Company) HRF ON b.inventoryid = hrf.inventoryid
JOIN
    inventorystatus m (NOLOCK) ON b.statusid = m.statusid

它正在使用2個表-

  • [dbo]。[FC03QTY] a

  • dbo.inventory b

在SKU級別加入。

[dbo].[FC03QTY]在草稿數據庫中,而dbo.inventory在報告數據庫中。

如果它們位於2個不同的數據庫上,如何獲取這些表的查詢?

任何建議都收到了。

在sql server中,表的語法為[database name].[schema name].[table name]

所以你需要這樣的東西:

SELECT A.*, B.*
FROM 
     Database1.dbo.Table1 as  A, 
     Database2.dbo.Table2 as  B

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM