Updated March 13, 2023
Introduction to SQL Variables
Variables in standard query language (SQL) are named locations in the memory which are used by the SQL query or program to manipulate the data. These named memory locations act as placeholders for the value of the variable. The size of the memory location depends upon the data type of variable. We can have multiple types of variables in SQL based on the data type of the data that it can hold such as varchar, text, int, numeric, date, timestamp, etc.
How to declare and initialize variables in SQL?
Initialization and declaration of variables in SQL differ slightly based on the SQL database management server you are using. For example, a variable declaration starts with @ for user-defined variables and @@ for system-defined variables in SQL Server and MS SQL whereas in PL/PgSQL the declaration does not involve such symbols. For more information and syntax, you can refer to the documentation of respective database management servers. In this exercise, we will be working on postgreSQL or pgSQL.
Here are some examples of variable declarations.
DECLARE
counter INTEGER ;
student_name VARCHAR(50) ;
email VARCHAR(225)';
So, a variable declaration starts with the keyword “DECLARE”, followed by variable_name and its data type. If you are familiar with variables in C, this should be a piece of cake for you.
Next, initialization is specifying an initial value for the variable. We can do it while declaring the variable as well as later on while using the variable.
When we initialize a variable during the declaration, it looks like something as shown below.
DECLARE
counter INTEGER := 101 ;
student_name VARCHAR(50) := 'Radhika Sharma' ;
email VARCHAR(225)' := '[email protected]';
The entire code snippet for variable declaration and initialization in pgSQL looks something as shown below.
DO $$
DECLARE
counter INTEGER := 101 ;
student_name VARCHAR(255) := 'Radhika Sharma';
email VARCHAR(225) := '[email protected]';
BEGIN
RAISE NOTICE 'The student name for counter 101 is : %', student_name;
END $$;
In the above example, we made an inline function, where we have declared three variables namely,
- counter with data type INTEGER with ‘101’ as the initial value
- student_name with data type varchar or varying character with ‘Radhika Sharma’ as its initialization
- the email with data type varchar or varying character with ‘[email protected]’ as an initial value
Examples of SQL Variables
Let’s discuss more examples to understand variable declaration, initialization, and working across some frequently used data types.
Example #1
Working with variables of integer data type
In this example, we have declared three variables x, y, and z, and initialized the former two. We have tried to assign z the result of x and y ‘s subtraction. The code snippet for the same looks as shown below.
Code:
DO $$
DECLARE
x integer := 30;
y integer := 20;
z integer;
BEGIN
z := x - y;
RAISE NOTICE 'Value of z: %', z;
END $$;
Output:
Example #2
Working with variables of DATE and TIME data types
Code:
DO $$
DECLARE
created_date DATE := NOW();
BEGIN
RAISE NOTICE 'This program was created on %', created_date;
END $$;
Output:
In this particular example, we have declared a variable of DATE data type and initialized it with current time using the built-in NOW() function. Let’s see some variations of the same.
Code:
DO $$
DECLARE
created_at TIME := NOW();
process_date DATE := '2020-04-30';
BEGIN
RAISE NOTICE 'This program was created at % and will be processed on %',created_at, process_date;
END $$;
Output:
In the above illustration, we have created the ‘created_at’ variable of the TIME data type. It is different from the date in the manner that it gives a timestamp without the date. Next, we have a ‘process_date’ variable which has an initialized date.
Example #3
Working with variables of NUMERIC data types
Most of the SQL database management servers provide for a variety of numeric data types. The most commonly used numeric data types are SMALLINT, INTEGER, BIGINT, REAL, DOUBLE PRECISION, and NUMERIC(p,s). The first three of the mentioned data types can only hold integer values. We have already discussed integers in example 1. In this section, we will discuss REAL, PRECISION, and NUMERIC(p,s) data types.
Code:
DO $$
DECLARE
x float := 40;
y real := 3;
z numeric(11,2);
BEGIN
z := x / y;
RAISE NOTICE 'Value of z: %', z;
END $$;
Output:
In the above example, we declared three types of variables. Everything is self-explanatory at this point except numeric(11,2) which stands for 11 digits before the precision or decimal point and 2 digits after it
Example #4
Copying data type of a column to a variable
Some SQL databases allow us to copy the data type of a column in the database table to a variable.
The syntax for the same in postgreSQL is as follows :
variable_name table_name.column_name%TYPE;
For illustration purposes consider the “product_details” table with columns like product_id, product_name, variations, price, etc.
Here, we want to copy the data type of column_name product_name to a variable called “new_product”.
Code:
DO $$
DECLARE
new_product product_details.product_name%type := 'Books';
BEGIN
RAISE NOTICE 'The recently added product in the store is %', new_product;
END $$;
Output:
Conclusion
Variables in any programming language acts are specific memory locations that act as placeholders for its actual value. This is true for SQL variables also. The size of the memory location depends upon the data type of the value that the particular variable can hold.
Recommended Articles
We hope that this EDUCBA information on “SQL Variables” was beneficial to you. You can view EDUCBA’s recommended articles for more information.