Заголовок



DECLARE @TypeID INT
SET @TypeID = 2

SELECT
CASE
        WHEN @TypeID = 0 THEN 'Тип=0'
        WHEN @TypeID = 1 THEN 'Тип=1'
        ELSE 'х.з.' END



IF NOT EXISTS (SELECT * FROM tab1 WHERE Param1 = 10)
BEGIN
        PRINT 'NOT EXISTS'
END



DECLARE @DateBegin datetime
DECLARE @DateEnd datetime

SET @DateBegin = 'aug 15 1969'
SET @DateEnd = 'aug 16 1969'

IF DATEDIFF(DAY, @DateBegin, @DateEnd) <> 1
BEGIN
    DECLARE @@DateBegin varchar(11)
    DECLARE @@DateEnd varchar(11)

    SET @@DateBegin = CAST(@DateBegin AS varchar(11))
    SET @@DateEnd = CAST(@DateEnd AS varchar(11))

    RAISERROR ('Разница между ''%s '' и ''%s '' не равна 1', 16, 1, @@DateBegin, @@DateEnd)
    RETURN
END



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