0
我正在使用遊標。我想創建一個光標來顯示每個客戶的信息以及他已經購買的產品。爲此目的,我寫了如下光標:幫助完成遊標
declare myCursor cursor
for select Customers.CustomerID, Customers.ContactName, Products.ProductName, SUM(Quantity) as Total
from Customers inner join Orders on Customers.CustomerID = Orders.CustomerID
inner join([Order Details] inner join Products on [Order Details].ProductID = Products.ProductID)
on Orders.OrderID = [Order Details].OrderID
group by Customers.CustomerID, Customers.ContactName, Products.ProductName
--
declare @CustomerID nchar(10), @ContactName nvarchar(30), @ProductName nvarchar(4), @Total int
open myCursor
fetch next from myCursor into @CustomerID, @ContactName , @ProductName, @Total
while @@FETCH_STATUS = 0
begin
begin
print @CustomerID+' '[email protected]+' '[email protected]+' '+CAST(@Total as varchar)
fetch next from myCursor into @CustomerID, @ContactName , @ProductName, @Total
end
end
現在它顯示每個客戶購買每件產品的次數。現在我想使用條件關鍵字,如IN,CONTAIN或EXISTS來完成我的光標以顯示每個客戶已購買的產品。但我不知道這樣做。你能幫我嗎?
爲什麼你想用光標來做到這一點? – 2010-09-01 10:50:16