I need to compare two tables and then filter out only those records that do not have a matching record.
I then need to return only a few of those rows.
To return rows I use…
SELECT *
FROM (SELECT ROW_NUMBER() OVER (ORDER BY Orderby ASC)
AS Row, * FROM vImportFiltered where userid <> @userID)
AS vImport
WHERE Row >= @NumbFrom AND Row <= @NumbTo
For the filter of records I use…
SELECT
*
FROM
ImportProducts IP Where IP.ImpProdNameID = @impProdNameID and IP.ImpProdCatID = @ImpProdCatID and
NOT EXISTS (SELECT *
FROM dbo.ProductDetails PD INNER JOIN
dbo.ProductsLU PL ON PD.LUProdSellUseID = PL.LUProdSellUseID
WHERE
IP.ImpProductID = PD.ProdImpID AND
(CONVERT(varchar(36),PL.UserID) = @userID))
Both work separately but when I try to combine them I get errors.
SELECT
*
FROM
(SELECT ROW_NUMBER() OVER (ORDER BY Orderby ASC)
AS Row, * ImportProducts IP Where IP.ImpProdNameID = @impProdNameID and IP.ImpProdCatID = @ImpProdCatID
and
NOT EXISTS (SELECT *
FROM dbo.ProductDetails PD INNER JOIN
dbo.ProductsLU PL ON PD.LUProdSellUseID = PL.LUProdSellUseID
WHERE
IP.ImpProductID = PD.ProdImpID AND
(CONVERT(varchar(36),PL.UserID) = @userID)) as vImport
WHERE Row >= @NumbFrom AND Row <= @NumbTo
Errors:
Msg 102, Level 15, State 1, Line 17
Incorrect syntax near 'ImportProducts'.
Msg 156, Level 15, State 1, Line 25
Incorrect syntax near the keyword 'as'.
I’ve also tried
WHERE
IP.ImpProductID = PD.ProdImpID AND
(CONVERT(varchar(36),PL.UserID) = @userID))And Row >= @NumbFrom AND Row <= @NumbTo
What am I doing wrong?
Thanks
You are missing a
FROM