我正在尝试执行以下存储过程,它需要永远执行它以获取更多记录。有什么方法可以用更好/更优化的方式来写这个
DECLARE productcursor CURSOR FOR
SELECT ProductId,
product_startdate
FROM Products
WHERE ProductStatus IN ('F', 'R', 'M')
GROUP BY productId
OPEN productcursor
FETCH NEXT FROM productcursor INTO @var_PId,@var_PStartDate
WHILE (@@FETCH_STATUS = 0)
BEGIN
SELECT @var_count = count(*) FROM Products
WHERE ProductID IN ((
SELECT ProductID FROM Products
WHERE ProductStatus IN ('OR', 'OP')
AND Comments LIKE '%CANCELLED%'
except ( SELECT ProductID FROM Products WHERE ProductStatus = 'DE'
UNION
SELECT ProductID FROM Products WHERE ProductStatus = 'OR' AND Comments NOT LIKE '%CANCELLED%'
))
except ( SELECT ProductID FROM Products
WHERE ProductStatus IN ('RE', 'C', 'S', 'D')
))
and productid = @var_PId
if @var_count = 0
INSERT INTO ProductDel
(ProductID, ProductDate, ProductDelStatus)
VALUES (@var_PID, @var_PStartDate, 'UNKNOWN')
else
INSERT INTO ProductDel
(ProductID, ProductDate, ProductDelStatus)
VALUES (@var_PID, @var_PStartDate, 'INService')
FETCH NEXT FROM productcursor INTO @var_PId,@var_PStartDate
END
Close productcursor
Deallocate productcursor
正如所建议的那样,我能够在没有光标的情况下完成这项工作,但仍然需要永远执行此工作...超过2小时的百万条记录 这是sql小提琴链接 http://sqlfiddle.com/#!6/b14eb/56