Hi, I am working on cursors. I want to create a cursor to display each customer’s information and the product that he has purchased more. For this aim, I wrote the following cursor:
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+' '+@ContactName+' '+@ProductName+' '+CAST(@Total as varchar)
fetch next from myCursor into @CustomerID, @ContactName , @ProductName, @Total
end
end
Now it displays that how many times each customers has purchased each product. Now I want to use condition keywords like IN, CONTAIN or EXISTS to complete my cursor to display the product that each customer has purchased more. But I have no idea to do it. Would you please help me?