Is there a way to UPDATE TOP (N) with inner join where N is a field of such inner join? I'm trying to create a script that synchronizes Sales and Inventory tables. For that I wrote an UPDATE on the Inventory table (which has 1 record per item of inventory present) like this:
UPDATE TOP (q.QuantitySold) i
SET i.Converted = 1,
    i.CartID = q.CartID,
    i.ReservedDate = GETDATE()
FROM Inventory i
INNER JOIN
(
    SELECT product.ProductID, sales.CartID, COUNT(sales.ID) AS QuantitySold
    FROM Products product
    INNER JOIN Sales sales ON sales.ProductID = product.ProductID
    WHERE <conditions> 
    GROUP BY product.ProductID, sales.CartID
) q ON q.ProductID = i.ProductID
WHERE i.Converted = 0 AND i.CartID IS NULL 

But it's not working, error says q.QuantitySold couldn't be bound.
Is there a way to update N records of inventory (equal to the quantity sold) without using a cursor? I refuse to give up like that. 
Note: this is a simplified version of the actual query.

You could use ROW_NUMBER to enumerate the inventory items that you need to update.
WITH cteProducts AS(
    SELECT product.ProductID, sales.CartID, COUNT(sales.ID) AS QuantitySold
    FROM Products product
    INNER JOIN Sales sales ON sales.ProductID = product.ProductID
    WHERE <conditions> 
    GROUP BY product.ProductID, sales.CartID
),
cteInventory AS(
    SELECT *,
ROW_NUMBER() OVER( PARTITION BY ProductID ORDER BY (SELECT NULL)) AS rn /*Change the ORDER BY for an actual column if needed, probably for FIFO*/
    FROM Inventory
    WHERE i.Converted = 0 
    AND i.CartID IS NULL 
)
UPDATE i
SET i.Converted = 1,
    i.CartID = q.CartID,
    i.ReservedDate = GETDATE()
FROM cteInventory i
INNER JOIN cteProducts q ON q.ProductID = i.ProductID
WHERE i.rn <= q.QuantitySold;