Declaring Variables in Oracle PLSQL

A variable, by definition, is a name given to a memory space.

In Oracle PLSQL a variable allows a programmer to store the needed data / values in variables while the program is being executed.

The Syntax for declaring a variable in Oracle PLSQL is:
variable_name [CONSTANT] data_type [NOT NULL] [:= DEFAULT initial value]

For example we can create a variable named ‘websitename’ in Oracle PLSQL as:

[sourcecode language=”sql”]
websitename VARCHAR2(100);[/sourcecode]

In the above statement we have just created a variable but the variable doesn’t have anything stored in it. We can assign a value to the above created ‘websitename’ variable as:

[sourcecode language=”sql”]
websitename := ‘Website Name Is techhoney.com’;[/sourcecode]

We can merge the two steps above and can create a variable along with assigning some value to it in a single statement as:

[sourcecode language=”sql”]
websitename VARCHAR2(100) := ‘Website Name Is techhoney.com’;[/sourcecode]

Later we can change the value of ‘websitename’ variable to something else e.g.

[sourcecode language=”sql”]
websitename := ‘This is just a test’;[/sourcecode]

Here we should understand that we have created a variable and not a constant; in case of constant the value assigned to it cannot be changed.

Below is an example is to create a constant in Oracle PLSQL:
[sourcecode language=”sql”]
grandTotal CONSTANT NUMERIC(10,1) := 123.456;[/sourcecode]

Note: Once assigned, the value of a constant cannot be changed.


One Reply to “Declaring Variables in Oracle PLSQL”

  1. I just want to say I’m newbie to blogs and really savored this blog site. Probably I’m going to bookmark your website . You certainly come with fabulous article content. With thanks for revealing your blog.

Leave a Reply

Your email address will not be published. Required fields are marked *