Declaring a local variable

    Available in

    PSQL

    Syntax

    The statement DECLARE [VARIABLE] is used for declaring a local variable. The keyword VARIABLE can be omitted. One DECLARE [VARIABLE] statement is required for each local variable. Any number of DECLARE [VARIABLE] statements can be included and in any order. The name of a local variable must be unique among the names of local variables and input and output parameters declared for the module.

    A local variable can be of any SQL type other than an array.

    • A domain name can be specified as the type and the variable will inherit all of its attributes.

    • If the TYPE OF *domain* clause is used instead, the variable will inherit only the domain’s data type, and, if applicable, its character set and collation attributes. Any default value or constraints such as NOT NULL or CHECK constraints are not inherited.

    • If the TYPE OF COLUMN *relation*.*column*> option is used to “borrow” from a column in a table or view, the variable will inherit only the column’s data type, and, if applicable, its character set and collation attributes. Any other attributes are ignored.

    The variable can be constrained NOT NULL if required. If a domain has been specified as the data type and already carries the NOT NULL constraint, it will not be necessary. With the other forms, including use of a domain that is nullable, the NOT NULL attribute should be included if needed.

    Unless specified, the character set and collation sequence of a string variable will be the database defaults. A CHARACTER SET clause can be included, if required, to handle string data that is going to be in a different character set. A valid collation sequence (COLLATE clause) can also be included, with or without the character set clause.

    Local variables are NULL when execution of the module begins. They can be initialized so that a starting or default value is available when they are first referenced. The DEFAULT <initvalue> form can be used, or just the assignment operator, ‘=’: = <initvalue>. The value can be any type-compatible literal or context variable.

    Be sure to use this clause for any variables that are constrained to be NOT NULL and do not otherwise have a default value available.

    1. AS
    2. -- Declaring a variable of the INT type
    3. -- Declaring a variable of the INT type that does not allow NULL
    4. DECLARE VARIABLE J INT NOT NULL;
    5. -- Declaring a variable of the INT type with the default value of 0
    6. DECLARE VARIABLE K INT DEFAULT 0;
    7. -- Declaring a variable of the INT type with the default value of 1
    8. -- Declaring a variable based on the COUNTRYNAME domain
    9. DECLARE FARM_COUNTRY COUNTRYNAME;
    10. -- Declaring a variable of the type equal to the COUNTRYNAME domain
    11. DECLARE FROM_COUNTRY TYPE OF COUNTRYNAME;
    12. -- Declaring a variable with the type of the CAPITAL column in the COUNTRY table
    13. DECLARE CAPITAL TYPE OF COLUMN COUNTRY.CAPITAL;
    14. BEGIN
    15. /* PSQL statements */

    See also