PL-SQL
Oracle PL/SQL Exception Handling: Examples to Raise User-defined Exception
What is Exception Handling in PL/SQL? An exception occurs when the PL/SQL engine encounters an...
In MS SQL, variables are the object which acts as a placeholder to a memory location. Variable hold single data value.
In this tutorial you will learn:
MS SQL has two types of variables:
However, the user can only create a local variable.
Below figure explain two types of variable available in MS SQL server.
TSQL Syntax:
DECLARE { @LOCAL_VARIABLE[AS] data_type [ = value ] } Rules:
Query: With 'AS'
DECLARE @COURSE_ID AS INT;
Query: Without 'AS'
DECLARE @COURSE_NAME VARCHAR (10);
Query: DECLARE two variables
DECLARE @COURSE_ID AS INT, @COURSE_NAME VARCHAR (10);
You can assign a value to a variable in the following three ways:
Let's have a look at all three ways in detail:
T-SQL Syntax:
DECLARE { @Local_Variable [AS] Datatype [ = value ] }Here, after datatype we can use '=' followed by value to be assigned
Query:
DECLARE @COURSE_ID AS INT = 5 PRINT @COURSE_ID
Sometimes we want to keep declaration and initialization separate. SET can be used to assign values to the variable, post declaring a variable.Below are the different ways to assign values using SET:
Example: Assigning a value to a variable using SET
Syntax:
DECLARE @Local_Variable <Data_Type> SET @Local_Variable = <Value>
Query:
DECLARE @COURSE_ID AS INT SET @COURSE_ID = 5 PRINT @COURSE_ID
Example: Assign a value to multiple variables using SET.
Syntax:
DECLARE @Local_Variable _1 <Data_Type>, @Local_Variable_2 <Data_Type>, SET @Local_Variable_1 = <Value_1> SET @Local_Variable_2 = <Value_2>
Rule: One SET Keyword can be used to assign a value to only one variable.
Query:
DECLARE @COURSE_ID as INT, @COURSE_NAME AS VARCHAR(5) SET @COURSE_ID = 5 SET @COURSE_NAME = 'UNIX' PRINT @COURSE_ID PRINT @COURSE_NAME
Example: Assigning a value to a variable with a Scalar Subquery using SET
Syntax:
DECLARE @Local_Variable_1 <Data_Type>, @Local_Variable_2 <Data_Type>,SET @Local_Variable_1 = (SELECT <Column_1> from <Table_Name> where <Condition_1>)
Rules:
Assumption: Assume that we have the table as 'gtupapers' with two columns as displayed below:
We will use 'gtupapers' table in the further tutorials
Example 1: When subquery return one row as a result.
DECLARE @COURSE_NAME VARCHAR (10) SET @COURSE_NAME = (select Tutorial_name from gtupapers where Tutorial_ID = 3) PRINT @COURSE_NAME
Example 2: When subquery returns zero row as a result
DECLARE @COURSE_NAME VARCHAR (10) SET @COURSE_NAME = (select Tutorial_name from gtupapers where Tutorial_ID = 5) PRINT @COURSE_NAME
In this particular case, the variable value is EMPTY, i.e., NULL.
Just like SET, we can also use SELECT to assign values to the variables, post declaring a variable using DECLARE. Below are different ways to assign a value using SELECT:
Example: Assigning a value to a variable using SELECT
Syntax:
DECLARE @LOCAL_VARIABLE <Data_Type> SELECT @LOCAL_VARIABLE = <Value>
Query:
DECLARE @COURSE_ID INT SELECT @COURSE_ID = 5 PRINT @COURSE_ID
Example: Assigning a value to multiple variable using SELECT
Syntax:
DECLARE @Local_Variable _1 <Data_Type>, @Local_Variable _2 <Data_Type>,SELECT @Local_Variable _1 = <Value_1>, @Local_Variable _2 = <Value_2>
Rules: Unlike SET, SELECT can be used to assign a value to multiple variables separated by the comma.
DECLARE @COURSE_ID as INT, @COURSE_NAME AS VARCHAR(5) SELECT @COURSE_ID = 5, @COURSE_NAME = 'UNIX' PRINT @COURSE_ID PRINT @COURSE_NAME
Example: Assigning the value to a variable with a Subquery using SELECT
Syntax:
DECLARE @Local_Variable_1 <Data_Type>, @Local_Variable _2 <Data_Type>,SELECT @Local_Variable _1 = (SELECT <Column_1> from <Table_name> where <Condition_1>)
Rules:
Example 1: When subquery return one row as a result.
DECLARE @COURSE_NAME VARCHAR (10) SELECT @COURSE_NAME = (select Tutorial_name from gtupapers where Tutorial_ID = 1) PRINT @COURSE_NAME
Example 2: When subquery return zero row as a result
DECLARE @COURSE_NAME VARCHAR (10) SELECT @COURSE_NAME = (select Tutorial_name from gtupapers where Tutorial_ID = 5) PRINT @COURSE_NAME
In this particular case, the variable is to EMPTY, i.e., NULL.
Example 3: Assign a value to a variable with a regular SELECT statement.
Syntax:
DECLARE @Local_Variable _1 <Data_Type>, @Local_Variable _2 <Data_Type>,SELECT @Local_Variable _1 = <Column_1> from <Table_name> where <Condition_1>
Rules:
Query 1: The query returns one row.
DECLARE @COURSE_NAME VARCHAR (10) SELECT @COURSE_NAME = Tutorial_name from gtupapers where Tutorial_ID = 3 PRINT @COURSE_NAME
Query 2: The query returns multiple rows.
DECLARE @COURSE_NAME VARCHAR (10) SELECT @COURSE_NAME = Tutorial_name from gtupapers PRINT @COURSE_NAME
In this special case, variable value is set to the value of the last row.
Query 3: The query returns zero rows.
DECLARE @COURSE_NAME VARCHAR (10) SELECT @COURSE_NAME = Tutorial_name from gtupapers where Tutorial_ID = 5 PRINT @COURSE_NAME
In this particular case, the variable is EMPTY, i.e., NULL.
Using variable in the query
Query:
DECLARE @COURSE_ID Int = 1 SELECT * from gtupapers where Tutorial_id = @COURSE_ID
'DECLARE @@ as VARCHAR (10)'
While using DECLARE
Using SET
USING SELECT
What is Exception Handling in PL/SQL? An exception occurs when the PL/SQL engine encounters an...
What is an Index? Indexes in MySQL sort data in an organized sequential way. They are created on...
What is CASE Statement? A CASE statement is similar to IF-THEN-ELSIF statement that selects one...
What is PL/SQL Datatypes? A data type is associated with the specific storage format and range...
$20.20 $9.99 for today 4.5 (125 ratings) Key Highlights of SQL Tutorial PDF 220+ pages eBook...
What is Trigger in PL/SQL? TRIGGERS are stored programs that are fired by Oracle engine...