Question : Can anyone help with some SQL syntax.

I have struggled with this for a couple of hours and have finally admitted defeat.

I have 3 Tables :
"TBL_Addresses" contains supplier details including an "Addr_OrderCurrency" field that identifies which currency a supplier normally uses.

"TBL_Stock" contains details of stock items, including a "Stock_SuppCode" field that links a stock record with a supplier from the addresses table.

"TBL_StockCosts" contains details of the costings of each stock item and includes the field "StkCost_StockCode" that links a costs record back to its host stock record and a "StkCost_FactoryCurrency" field that identifies the currency of the item's cost price.

The code I have attached has allowed me to identify instances where the currency held in a supplier's address record (TBL_Addresses.Addr_OrderCurrency) does not match the currency held in a costs record (TBL_StockCosts.StkCost_FactoryCurrency) for stock items supplied by that supplier.

Where I am stuck is in producing an "UPDATE" command to UPDATE the address record currency
to become the same as the cost price currency in these cases.
I think it is slightly complicated by the fact that the data might have become messy to the extent that there might be a few different stock items belonging to the supplier that have different cost currencies. I have experimented with "DISTINCT" and "TOP 1" expressions but have not been successful. Can anyone help?

Many thanks.
Code Snippet:
1:
2:
3:
4:
5:
6:
SELECT ADDR.Addr_Code, ADDR.Addr_Name, ADDR.Addr_OrderCurrency, STK.Stock_Code, STK.Stock_SuppCode, COST.StkCost_FactoryCurrency

FROM TBL_Stock AS STK INNER JOIN TBL_StockCosts AS COST ON STK.Stock_Code = COST.StkCost_StockCode
LEFT OUTER JOIN TBL_Addresses AS ADDR ON (ADDR.Addr_Kind = 'S') AND (ADDR.Addr_Code = STK.Stock_SuppCode)

WHERE COST.StkCost_FactoryCurrency <> ADDR.Addr_OrderCurrency

Answer : Can anyone help with some SQL syntax.

You should be able to use a subquery to select the top 1 StkCost_FactoryCurrency in the update. You will still have some entries in the COST.StkCost_FactoryCurrency that do not match the TBL_Addresses table if there were multiple cost records for the same supplier using different currencies.  I have attached the update statement.
1:
2:
3:
4:
5:
6:
7:
UPDATE ADDR
SET	   ADDR.Addr_OrderCurrency = (SELECT TOP 1 TBL_StockCosts.StkCost_FactoryCurrency
	FROM TBL_StockCosts
	WHERE TBL_StockCosts.StkCost_StockCode = COST.StkCost_StockCode)
FROM TBL_Stock AS STK INNER JOIN TBL_StockCosts AS COST ON STK.Stock_Code = COST.StkCost_StockCode
LEFT OUTER JOIN TBL_Addresses AS ADDR ON (ADDR.Addr_Kind = 'S') AND (ADDR.Addr_Code = STK.Stock_SuppCode)
WHERE COST.StkCost_FactoryCurrency <> ADDR.Addr_OrderCurrency
Random Solutions  
 
programming4us programming4us