Thank you Ifor! I didn't expect to get something further on this.
Yes your script is better, thank you for tidying it up.
This is the dbo.lewis_invoicelineview
SELECT dbo.tblCustomers.CustomerID, dbo.tblCustomers.Customer, dbo.tblInvoices.InvoiceNumber, dbo.tblInvoiceItemss.InvoiceItemID, dbo.tblInvoiceItemss.BatchNumber, dbo.tblInvoices.InvoiceDate, dbo.tblInvoiceItemss.BSLPartNo,
dbo.tblInvoiceItemss.UnitPrice, dbo.tblInvoiceItemss.Qty, dbo.tblInvoiceItemss.Qty / dbo.tblInvoiceItemss.UnitOfMeasure * dbo.tblInvoiceItemss.UnitPrice AS Total_price, dbo.tblInvoiceItemss.UnitDescription,
dbo.tblInvoiceItemss.UnitOfMeasure
FROM dbo.tblCustomers INNER JOIN
dbo.tblInvoices ON dbo.tblCustomers.CustomerID = dbo.tblInvoices.CustomerID INNER JOIN
dbo.tblInvoiceItemss ON dbo.tblInvoices.InvoiceNumber = dbo.tblInvoiceItemss.InvoiceNumber INNER JOIN
dbo.tblParts ON dbo.tblInvoiceItemss.BSLPartNo = dbo.tblParts.BSLPartNumber
Then this is dbo.lewis_bomfin
WITH PartsCTE
AS
(
SELECT NULL AS BOMID, Level = 0, BSLPartNumber, NULL AS MaterialID, CONVERT(varchar, '') AS ParentBSLPartNumber,
CONVERT(varchar, BSLPartNumber) AS Path,
NULL AS TopLevelBOMID,
NULL AS BottomLevelBOMID,
CONVERT(decimal(8,2), 1) AS TotalQuantity,
NULL As OpNo
FROM tblParts
WHERE (BSLPartNumber IS NULL OR tblParts.BSLPartNumber = BSLPartNumber)
AND ISNULL(BoughtOutComponent, 0) = 0
UNION ALL
SELECT b.BOMID, c.Level + 1 AS Level, b.BSLPartNumber, b.MaterialID, CONVERT(varchar, b.ParentBSLPartNumber) AS ParentBSLPartNumber,
CONVERT(varchar, CASE WHEN b.BSLPartNumber IS NULL THEN Path ELSE Path + '-' + b.BSLPartNumber END) AS Path,
CASE WHEN c.Level = 0 THEN b.BOMID ELSE TopLevelBOMID END AS TopLevelBOMID,
CASE WHEN b.MaterialID IS NULL THEN b.BOMID ELSE BottomLevelBOMID END AS BottomLevelBOMID,
CONVERT(decimal(8,2), b.Quantity * TotalQuantity) AS TotalQuantity,
CASE WHEN b.OpNo IS NOT NULL THEN b.OpNo ELSE c.OpNo END AS OpNo
FROM tblPartsBOM b
INNER JOIN PartsCTE c ON b.ParentBSLPartNumber = c.BSLPartNumber
)
SELECT Level,
topbom.ParentBSLPartnumber AS BSLPartNumber,
topbom.BSLPartNumber AS TopLevelPart,
topbom.OpNo AS TopLevelOpNo,
botbom.BSLPartNumber AS BottomLevelPart,
MaterialSize,
CASE WHEN p.BoughtOut = 1 THEN bom.BSLPartNumber ELSE NULL END AS BOP,
dbo.FinishedStock(topBOM.ParentBSLPartNumber) AS QuantityInStock,
dbo.LatestMaterialPrice(m.MaterialID) AS MaterialPrice,
dbo.LatestMaterialPricePer(m.MaterialID) AS MaterialPricePer,
dbo.LatestMaterialFreeIssue(m.MaterialID) AS FreeIssueMaterial,
dbo.LatestBOPPrice(bom.BSLPartNumber) AS BOPPrice,
dbo.LatestBOPPricePer(bom.BSLPartNumber) AS BOPPricePer,
bom.Per,
CASE WHEN bom.MaterialID IS NOT NULL THEN m.UnitOfMeasure ELSE 'part(s)' END AS Unit,
TotalQuantity AS TotalQuantityBOM
FROM PartsCTE
INNER JOIN tblPartsBOM topbom ON PartsCTE.TopLevelBOMID = topbom.BOMID
LEFT OUTER JOIN tblPartsBOM botbom ON PartsCTE.BottomLevelBOMID = botbom.BOMID
INNER JOIN tblPartsBOM bom ON PartsCTE.BOMID = bom.BOMID
LEFT OUTER JOIN tblParts p ON p.BSLPartNumber = PartsCTE.BSLPartNumber
LEFT OUTER JOIN tblMaterials m ON PartsCTE.MaterialID = m.MaterialID
WHERE (ISNULL(p.BoughtOutComponent, 0) = 1 OR m.MaterialID IS NOT NULL) --AND pb.DateCompleted IS NULL
AND dbo.FinishedStock(topbom.ParentBSLPartNumber) > 0
It times out within SQL, I think because these views aren't great and then referencing them is slowing the main view down.
From what you said earlier. Could these be incorporated to put in one view so it doesn't time out?
If you need more information or anything further from me, please let me know. Thanks again! You're a genius.