我有这个游标(SQL SERVER 2008 R2)来找出每个产品属于哪个类别。但有一个问题是某些产品没有任何子类别和类别(产品可以加入子类别,而saubcategory可以加入类别)。所以所有产品都不属于同类产品。我想知道如何在这个游标中放置一个IF / ELSE语句,如果Production.ProductCategory = null,那么它将把它放在另一个类别'No category!'中。 任何想法怎么??
--** Force to use advantureworks
USE AdventureWorks
GO
--** Declaring Category Cursor variables
--** catName is for the CATEGORY NAME and count is a counter to count the row of output
DECLARE @catName VARCHAR(20), @count int
--** Declaring Category Cursor it self(to loop on categiries) and the SELECT statement
DECLARE catCursor CURSOR FOR
SELECT Production.ProductCategory.Name FROM Production.ProductCategory
--** Opening cursor and fetching data from it
OPEN catCursor
FETCH NEXT FROM catCursor INTO @catName
--** Starting the loop in curser and filling the data
SET @count = 1
WHILE(@@FETCH_STATUS = 0)
BEGIN
PRINT N'Category Name: ' + CAST(@catName AS NVARCHAR)
PRINT N'Description: No description'
PRINT N'Product List: '
PRINT N'Row NO. Product ID Product Name Unit Price($) Is Top 10'
PRINT '====== ========== ===================== ========== ========='
--** Second cursor for products start here
--** Declaring Product Cursor variables
--** prodID, prodName and prodUnitPrice are what assignment wants from us
DECLARE @prodID INT, @prodName VARCHAR(30), @prodUnitPrice VARCHAR(20), @isTopTen VARCHAR (3)
--** Declaring Product Cursor it self(to loop on all products) and the SELECT statement
DECLARE prodCursor CURSOR FOR
--** HERE THERE MIGHT BE A CORRET SELEC STATEMENT WHICH STILL I DON'T KNOW WHAT IS !!!
--** I'VE WRITTEN SOMETHING WHICH IS NOT CORRECT BUT I PUT IT HERE IN COMMENTS:
SELECT DISTINCT A.ProductID, A.Name, SUM(ISNULL(SOD.UnitPrice,0))/COUNT(*) 'Unit Price' , A.InTopTen
FROM
(SELECT P.ProductID, P.Name,PC.Name catName,
CASE WHEN X.ProductID IS NULL THEN 'NO' ELSE 'YES' END AS InTopTen
FROM Production.Product P
LEFT JOIN Production.ProductSubcategory PSC ON P.ProductSubcategoryID = PSC.ProductSubcategoryID
LEFT JOIN Production.ProductCategory PC ON PSC.ProductCategoryID = PC.ProductCategoryID
LEFT JOIN
(
SELECT TOP 10 ProductID FROM Sales.SalesOrderDetail SOD
GROUP BY ProductID
ORDER BY COUNT(*) DESC
) X
ON X.ProductID = P.ProductID) A LEFT JOIN Sales.SalesOrderDetail SOD ON A.ProductID = SOD.ProductID
WHERE A.catName = CAST(@catName AS NVARCHAR)
GROUP BY A.ProductID , A.Name, A.InTopTen
ORDER BY A.ProductID
--** Opening cursor and fetching data from it
OPEN prodCursor
FETCH NEXT FROM prodCursor INTO @prodID , @prodName , @prodUnitPrice , @isTopTen
--** Starting the loop in curser and filling the data
WHILE(@@FETCH_STATUS = 0)
BEGIN
PRINT CAST(@count AS NVARCHAR)
+' '+ CAST(@prodID AS NVARCHAR)
+' '+ CAST(@prodName AS NVARCHAR)
+' ' + CAST(@prodUnitPrice AS NVARCHAR)
+' ' + CAST(@isTopTen AS NVARCHAR)
SET @count = @count+1
--** Fetching next data from product cursor
FETCH NEXT FROM prodCursor INTO @prodID , @prodName , @prodUnitPrice , @IsTopTen
--** End of product cursor
END
CLOSE prodCursor
DEALLOCATE prodCursor
--*******************
PRINT '===================================================================================='
--** Fetching next data from category cursor
FETCH NEXT FROM catCursor INTO @catName
--** End of category cursor
END
CLOSE catCursor
DEALLOCATE catCursor
--=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* END OF QUERY =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
答案 0 :(得分:1)
尝试使用:
DECLARE catCursor CURSOR FOR
SELECT Production.ProductCategory.Name FROM Production.ProductCategory
UNION ALL
SELECT 'No Category'
然后将您的WHERE
子句更改为
WHERE ISNULL(A.catName, 'No Category') = CAST(@catName AS NVARCHAR)