Variable in SQL Server

Variables are important factor of any programming or scripting language. The variables very useful while creating store procedure.

Declaration of variable

The DECLARE keyword is used to declare variables and the variable name is prefixed with '@' character in SQL Server.

For example:

DECLARE @Stud_Name varchar (20)

In above example, @Stud_Name is the variable name that is a varchar type and its size is 20.

SET keyword is used to set a value into the variable as:

SET @Stud_Name = 'James'

PRINT keyword is used to print the value stored into the variable as given below.
        
DECLARE @Stud_Name varchar (20)
SET @Stud_Name = 'James'
PRINT @Stud_Name