T-SQL, Transact SQL, Курсор, Цикл, Объявление переменных
DECLARE @ProdID INT
DECLARE @CompID INT
DECLARE @Fetches INT
--=================================================================
--Условие выхода из цикла - @@FETCH_STATUS
DECLARE Cur CURSOR READ_ONLY FOR
SELECT ProdID, CompID FROM Sales
OPEN Cur
FETCH NEXT FROM Cur INTO @ProdID, @CompID
WHILE (@@FETCH_STATUS <> -1) BEGIN
PRINT @ProdID
PRINT @CompID
FETCH NEXT FROM Cur INTO @ProdID, @CompID
END
CLOSE Cur
DEALLOCATE Cur
--=================================================================
--Условие выхода из цикла - сравнение с @@CURSOR_ROWS
DECLARE Cur CURSOR READ_ONLY FOR
SELECT ProdID, CompID FROM Sales
OPEN Cur
SET @Fetches = 0
FETCH NEXT FROM Cur INTO @ProdID, @CompID
WHILE (@Fetches <> @@CURSOR_ROWS) BEGIN
PRINT @ProdID
PRINT @CompID
SET @Fetches = @Fetches + 1
FETCH NEXT FROM Cur INTO @ProdID, @CompID
END
CLOSE Cur
DEALLOCATE Cur
DECLARE @CompID INT
DECLARE @Fetches INT
--=================================================================
--Условие выхода из цикла - @@FETCH_STATUS
DECLARE Cur CURSOR READ_ONLY FOR
SELECT ProdID, CompID FROM Sales
OPEN Cur
FETCH NEXT FROM Cur INTO @ProdID, @CompID
WHILE (@@FETCH_STATUS <> -1) BEGIN
PRINT @ProdID
PRINT @CompID
FETCH NEXT FROM Cur INTO @ProdID, @CompID
END
CLOSE Cur
DEALLOCATE Cur
--=================================================================
--Условие выхода из цикла - сравнение с @@CURSOR_ROWS
DECLARE Cur CURSOR READ_ONLY FOR
SELECT ProdID, CompID FROM Sales
OPEN Cur
SET @Fetches = 0
FETCH NEXT FROM Cur INTO @ProdID, @CompID
WHILE (@Fetches <> @@CURSOR_ROWS) BEGIN
PRINT @ProdID
PRINT @CompID
SET @Fetches = @Fetches + 1
FETCH NEXT FROM Cur INTO @ProdID, @CompID
END
CLOSE Cur
DEALLOCATE Cur