Assignment statements are important to any programming language. They allow the programmer to manipulate the data. Assignment statements can be used to store or transfer the value of an expression into a variable.

An expression is defined as a computation which returns a value. When assignment is used, these expressions are stored by identifier variables. The identifier is the name of a variable. For example, the syntax to use an assignment statement is:

  identifier := expression;
   

The colon and equal sign (:=) in the syntax shows that the expression is being stored by the identifier variable. It is then terminated with a semicolon (;).

Assignment can also be used to specify a value for a variable or row/record field. These are some examples of assignment statements to a variable that has already been declared:

Example 9-12. PL/pgSQL Assignment Statements

    ...
  BEGIN
    -- assigns 120 to the author_id variable
    author_id := 120;

    -- assigns the result of a multiplication 
    -- into the tax variable
    tax := subtotal * 0.06;
  END;
  

Note

You should keep in mind that if the expression type differs from the variable data type, the PL/pgSQL parser will generate some run time errors.

To call a function, use the execute SELECT query or do an assignment. An assignment will cause an internal PL/pgSQL SELECT to execute. After execution, it will return a value. All functions in PostgreSQL returns a value. If you did not want to return a result from the function call, then specify PERFORM :

 PERFORM query