I’m trying to create a stored procedure that retrieves data from 3 tables on 2 different servers. This is the select statement that I have.
SELECT InvoiceLine.BranchNo, InvoiceLine.Type_IN_CR, InvoiceLine.Docket,InvoiceLine.ProductCode,
InvoiceLine.Inv_Price * (select OuterUnits From server.PopTables.dbo.Products Inner Join InvoiceLine ON server.PopTables.dbo.Products.ProductCode = InvoiceLine.ProductCode Where server.PopTables.dbo.Products.ProductCode = InvoiceLine.ProductCode) AS PricePT,
InvoiceLine.Inv_Quantity * (select OuterUnits From server.PopTables.dbo.Products Inner Join InvoiceLine ON server.PopTables.dbo.Products.ProductCode = InvoiceLine.ProductCode Where server.PopTables.dbo.Products.ProductCode = InvoiceLine.ProductCode) AS QunatityPT,
InvoiceLine.Inv_Total, InvoiceHeader.InvoiceDate
It is returning the Error below 4 times.
Msg 4104, Level 16, State 1, Procedure IMFertiliserRebates, Line 7
The multi-part identifier “PROGRAMS.PopTables.dbo.Products.ProductCode” could not be bound.
I’m trying to multiply two columns in one table by a column in another table on another server based on the product code.
I’m very lost!
Any help would be appreciated
Thanks.
You need to use tables aliases on your linked tables
e.g.
Because your original SQL is kinda large here are the changes that I made
Without Alias
ON server.PopTables.dbo.Products.ProductCode = invoiceline.productcodeWHERE server.PopTables.dbo.Products.ProductCode...ON serverb.table3.dbo.productsinner = invoiceline.productcodeWHERE serverb.table3.dbo.productsinner.productcode = invoiceline.productcodeWith alias
ON p.ProductCode = invoiceline.productcodeWHERE p.ProductCode...ON serverb.table3.dbo.productsinner = invoiceline.productcodeWHERE serverb.table3.dbo.productsinner.productcode = invoiceline.productcode