我想查詢一個表格並將結果導入變量。使用變量結果做另一個選擇使用變量細節作爲過濾器的查詢。創建一個sql變量並查詢其他表格
到目前爲止:
DECLARE @storeIds int
SET @storeIds = (SELECT StoreID FROM Store WHERE ParentStoreID=9)
--print @storeIds
SELECT c.FirstName, c.LastName, c.CustomerId, r.StoreID
FROM Consumer AS c
INNER JOIN Purchases AS r ON c.CustomerId= r.CustomerId
WHERE r.StoreID = @storeIds
-- (r.StoreID = 9) OR
-- (r.StoreID = 10) OR
-- (r.StoreID = 11)
GROUP BY c.FirstName, c.LastName, c.CustomerId, r.StoreID
ORDER BY c.FirstName
我得到一個錯誤:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
比ks巴里!我不知道我可以在WHERE內創建另一個選擇語句:( –