postgresql_16:6.6.5.1 Assignment
PostgreSQL 16 > Server Programming > PL/pgSQL — SQL Procedural Language > Basic Statements > Assignment
An assignment of a value to a PL/pgSQL variable is written as: *variable* { := | = } *expression*; As explained previously, the expression in such a statement is evaluated by means of an SQL SELECT command sent to the main database engine. The expression must yield a single value (possibly a row value, if the variable is a row or record variable). The target variable can be a simple variable (optionally qualified with a block name), a field of a row or record target, or an element or slice of an array target. Equal (=) can be used instead of PL/SQL-compliant :=.
If the expression's result data type doesn't match the variable's data type, the value will be coerced as though by an assignment cast (see (see postgresql_16:3.7.4)). If no assignment cast is known for the pair of data types involved, the PL/pgSQL interpreter will attempt to convert the result value textually, that is by applying the result type's output function followed by the variable type's input function. Note that this could result in run-time errors generated by the input function, if the string form of the result value is not acceptable to the input function.
Examples: tax := subtotal * 0.06; my_record.user_id := 20; my_array[j] := 20; my_array[1:3] := array[1,2,3]; complex_array[n].realpart = 12.3;