DECLARE @catId INT,
@catName VARCHAR(50);
DECLARE curCate CURSOR FOR SELECT catId,catName FROM dbo.TestCategory
OPEN curCate
FETCH NEXT FROM curCate INTO @catId,@catName
WHILE(@@FETCH_STATUS=0)
BEGIN
PRINT @catName;
DECLARE @proId INT,
@proName VARCHAR(50)
DECLARE curPro CURSOR FOR SELECT proId,proName FROM dbo.TestProduct WHERE cateId=@catId
OPEN curPro
FETCH NEXT FROM curPro INTO @proId,@proName
WHILE(@@FETCH_STATUS=0)
BEGIN
PRINT CONVERT(VARCHAR,@proId)+' '+@catName+' '+@proName;
FETCH NEXT FROM curPro INTO @proId,@proName
END
CLOSE curPro
DEALLOCATE curPro
FETCH NEXT FROM curCate INTO @catId,@catName
END
CLOSE curCate

DEALLOCATE curCate

嵌套游标里的变量名或表的重命名,不要重复