sql local variable code example
Example 1: sql declare variable
DECLARE @MyCounter int;
SET @MyCounter = 0;
Example 2: sql create a variable in select
CREATE TABLE TestTable (cola int, colb char(3));
GO
SET NOCOUNT ON;
GO
DECLARE @MyCounter int;
SET @MyCounter = 0;
WHILE (@MyCounter < 26)
BEGIN;
INSERT INTO TestTable VALUES
(@MyCounter,
CHAR( ( @MyCounter + ASCII('a') ) )
);
SET @MyCounter = @MyCounter + 1;
END;
GO
SET NOCOUNT OFF;
GO
SELECT cola, colb
FROM TestTable;
GO
DROP TABLE TestTable;
GO
Example 3: local variables in sql
Local variables:
These variables can be used or exist
only inside the function. These variables
are not used or referred by any other function.
Global variables:
These variables are the variables which
can be accessed throughout the program.
Global variables cannot be created
whenever that function is called.