VARIABLES :- Variables are particular amount of memory area that are used to store the data values required to get producing the desired output from the program or SQL query . But Before using or allowing these variable to hold the specified data values we must declare them . The Declaration Statement of variable also tells the type of data that the variable will hold.
In SQL Variables can be declared with keyword Declare as shown below :-
1. DECLARING SINGLE VARIABLE AT ONE TIME
Declare @Variable-Name Data-Type
* HERE, DECLARE is a keyword that tells the interpreter that we are going to create variable
* DECLARE keyword is followed by Variable-Name that is valid name in which the value assigned to variable will be stored. Variable name is precedded by '@'
* Data-Type is used to specify the type of value that the variable will hold
EXAMPLE :-
Declare @firstname varchar(30)
Declare @midname varchar(20)
Declare @lastname varchar(10)
INITIALIZE VARIABLES IN SQL
After a variable is declared now the memory area is allocated to it. Now its time to assign value or intialize value of variable For Initializing variable in SQL we use a special keyword 'set' or 'select'
Before initializing the variable it consist of NULL value . If you try to print value of variable without initializing it . Then it will print some Garbage value .
SYNTAX :-
set @variable-Name = variable-value
OR
select @variable-naem =variable-value
* Here, 'set' or 'select' are keywords used for initializing values of variables
* variable-Name is name of variable . It should be same as you have given in declaration section
* variable-value is desired value you want to assign to variable
EXAMPLE :-
Declare @firstname varchar(30)
Declare @midname varchar(20)
Declare @lastname varchar(10)
set @firstname = 'geeksprogrammings'
set @midname = '@blogspot'
set @lastname = '.in'
PRINT VARIABLE VALUES
print the variable values means to display the data that is variable is holding and display it to computer screen
EXAMPLE :-
print @firstname
print @midname
print @lastname
FINAL OUTPUT :-
FINAL OUTPUT :-
2. DECLARING MULTIPLE VARIABLE IN SINGLE LINE
Declare @Variable-Name1 Data-Type , Variable-Name2 Data-Type -------UPTO N VARIABLE
* HERE, DECLARE is a keyword that tells the interpreter that we are going to create variable
* DECLARE keyword is followed by Variable-Names that is valid name in which the value assigned to variable will be stored. Variable name is precedded by '@'
* In SQL we can create multiple number of variable in single line by separating them by commas ( , ) .
* Data-Type is used to specify the type of value that the variable will hold
Declare @firstname varchar(10) , @midname varchar(10), @lastname varchar(10)
INITIALIZE MULTIPLE VARIABLES IN SINGLE LINE IN SQL
Initialization of variables in this section is similar to intializing them is previous section . But if we want to initialize multiple variables in single statement that does not supported by 'set' keyword as it is only used for initializing values of single variable at one line. So to fullfill our task we will use select keyword as shown in example below.
select @variable-name1 =variable-value , @variable-name-n = variable-value
* Here, 'set' or 'select' are keywords used for initializing values of variables
* variable-Name is name of variable . It should be same as you have given in declaration section
* variable-value is desired value you want to assign to variable
INITIALIZE MULTIPLE VARIABLES IN SINGLE LINE IN SQL
EXAMPLE :-
Declare @firstname varchar(10) , @midname varchar(10), @lastname varchar(10)
select @firstname = 'geeksprogrammings' , @midname = '@blogspot' , @lastname = '.in'
PRINT
MULTIPLE VARIABLE VALUES IN SINGLE LINE IN SQL
print @variable-name1 + @variable-name2 + @variable-name-n
print the variable values means to display the data that is variable is holding and display it to computer screen
PRINT MULTIPLE VARIABLE VALUES IN SINGLE LINE IN SQL
EXAMPLE :-
print @firstname
print @midname
print @lastname
FINAL OUTPUT :-
FINAL OUTPUT :-
0 Comment to "Declare Variable in SQL"
Post a Comment