7.6.3. DECLARE VARIABLE

Used for

Declaring a local variable

Available in

PSQL

Syntax

  1. DECLARE [VARIABLE] varname
  2. {<datatype> | domain | TYPE OF {domain | COLUMN rel.col}
  3. [NOT NULL] [CHARACTER SET charset] [COLLATE collation]
  4. [{DEFAULT | = } <initvalue>];
  5. <datatype> ::=
  6. {SMALLINT | INTEGER | BIGINT}
  7. | {FLOAT | DOUBLE PRECISION}
  8. | {DATE | TIME | TIMESTAMP}
  9. | {DECIMAL | NUMERIC} [(precision [, scale])]
  10. | {CHAR | CHARACTER [VARYING] | VARCHAR} [(size)]
  11. [CHARACTER SET charset]
  12. | {NCHAR | NATIONAL {CHARACTER | CHAR}} [VARYING]
  13. [(size)]
  14. | BLOB [SUB_TYPE {subtype_num | subtype_name}]
  15. [SEGMENT SIZE seglen] [CHARACTER SET charset]
  16. | BLOB [(seglen [, subtype_num])]
  17. <initvalue> ::= <literal> | <context_var>
Table 83. DECLARE VARIABLE Statement Parameters
ArgumentDescription

varname

Name of the local variable

datatype

An SQL data type

domain

The name of an existing domain in this database

rel.col

Relation name (table or view) in this database and the name of a column in that relation

precision

Precision. From 1 to 18

scale

Scale. From 0 to 18, it must be less than or equal to precision

size

The maximum size of a string in characters

subtype_num

BLOB subtype number

subtype_name

BLOB subtype mnemonic name

seglen

Segment size, not greater than 65,535

initvalue

Initial value for this variable

literal

Literal of a type compatible with the type of the local variable

context_var

Any context variable whose type is compatible with the type of the local variable

charset

Character set

collation

Collation sequence

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.

Data Type for Variables

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.

NOT NULL Constraint

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.

CHARACTER SET and COLLATE clauses

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.

Initializing a Variable

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.

Examples of various ways to declare local variables

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

See also

Data Types and Subtypes, Custom Data Types — Domains, CREATE DOMAIN