简体   繁体   中英

Select qry to using 2 databases

I have the below query:

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

It is using 2 tables -

  • [dbo].[FC03QTY] a

and

  • dbo.inventory b

that are joined at the SKU level.

[dbo].[FC03QTY] is on the scratch database and dbo.inventory is on the reports database.

How can I get my query to use these tables if they are on 2 different db?

Any advice greatly received.

In sql server the syntaxis for tables is [database name].[schema name].[table name]

So you need something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM