I would like to know if it's possible to create a Count variable like you would in C#.
DECLARE @Count Int
SET @Count = 0
--something happens
SET @Count += 1
--something happens
SET @Count += 1
IF @Count < 3
BEGIN
--Do something
END
In SQL Server 2008+ your code is perfectly valid:
DECLARE @Count INT;
SET @Count = 0;
PRINT @Count;
SET @Count += 1;
PRINT @Count;
SET @Count += 1;
IF @Count < 3
BEGIN
PRINT @Count;
END
With version before 2008 you can use full syntax:
SET @Count = @Count + 1;
Adds two numbers and sets a value to the result of the operation. For example, if a variable @x equals 35, then @x += 2 takes the original value of @x, add 2 and sets @x to that new value (37).
x = x + 1syntax. What's the purpose?