You could trivially replace the fragments:
v_InventoryMaster.INV_PK NOT IN (
SELECT DISTINCT v_InventoryMaster.INV_PK
FROM ecrs.v_InventoryMaster, ecrs.StockInventoryLinkDiscounts
WHERE v_InventoryMaster.INV_PK = StockInventoryLinkDiscounts.ILD_INV_FK
AND StockInventoryLinkDiscounts.ILD_DIS_FK = value -- value dynamically chosen by user
)
By:
NOT EXISTS (
SELECT *
FROM ecrs.StockInventoryLinkDiscounts sild
WHERE sild.ILD_INV_FK = tbl.INV_PK
AND sild.ILD_DIS_FK = value -- value dynamically chosen by user
-- ^^^^^ NOTE: this should probably be sild.ILD_DIS_PK
)
With tbl
being the correlation name for the outer query; the outer query would become:
SELECT DISTINCT v_InventoryMaster.INV_ScanCode
, v_InventoryMaster.INV_ReceiptAlias
FROM ecrs.v_InventoryMaster tbl
Also note that I removed the ecrs.v_InventoryMaster
table from the subquery, since it is already present in the outer query, and would result in exactly the same row(s) being checked as the outer query already has found.
This will give the complete query as:
SELECT DISTINCT v_InventoryMaster.INV_ScanCode
, v_InventoryMaster.INV_ReceiptAlias
FROM ecrs.v_InventoryMaster tbl
WHERE NOT EXISTS (
SELECT *
FROM ecrs.StockInventoryLinkDiscounts sild
WHERE sild.ILD_INV_FK = tbl.INV_PK
AND sild.ILD_DIS_FK = 6
)
OR NOT EXISTS (
SELECT *
FROM ecrs.StockInventoryLinkDiscounts sild
WHERE sild.ILD_INV_FK = tbl.INV_PK
AND sild.ILD_DIS_FK = 14
)
OR NOT EXISTS (
SELECT *
FROM ecrs.StockInventoryLinkDiscounts sild
WHERE sild.ILD_INV_FK = tbl.INV_PK
AND sild.ILD_DIS_FK = 25
)
;
My guess is that the parser is confused by the unaliased references to ecrs.v_InventoryMaster
. Another possibility is that the range table is full (if you have a lot of subquery-terms)