Variables are declared in the body of a batch or procedure with the DECLARE statement and are assigned values by using either a SET or SELECT statement.
Cursor variables can be declared with this statement and used with other cursor-related statements. After declaration, all variables are initialized as NULL, unless a value is provided as part of the declaration.
The Transact-SQL shown above declares two variables one called @TableName that holds the name of the table to retrieve data from. The other, called @SQL, holds the Transact-SQL statement that is built dynamically. After @TableName is assigned the value of SalaryHistory, the Transact-SQL statement is built dynamically. The value stored in @TableName is appended to SELECT * FROM, resulting in
SELECT * FROM SalaryHistory. This is what is executed by SQL Server.