Posted on 22nd July 2024|30 views
How to declare a variable in PostgreSQL?
Posted on 22nd July 2024| views
Syntax:
variable_name data_type [:= expression];
For example:
first_name VARCHAR(50) := 'Richard';
last_name VARCHAR(50) := 'Rambo';
payment NUMERIC(11,2) := 150.5;
Alias is used mostly for allotting a new name to a predetermined name.
This method changes the predetermined name to a new name for user convenience.
Syntax:
newname ALIAS FOR oldname;
To change the old name to the new user-desired name, we use this method.
For example:
User_number ALIAS FOR UserID;
Hence this will update the user number to userid.
This function mainly used for declaring variables that hold similar data type values.
Without knowing the data type also, we can declare the variable in the column as it stores the value in the form of that column data type.
%TYPE will declare the data type of the column to the new variable assigned.
%TYPE used for polymorphic functions which change their data type from one form to another every time you run a program.
Syntax:
variable_name table_name.column_name%TYPE;
For example:
New_state_name states.name%TYPE := 'Hyderabad';