Declaration statements

The general syntax of a variable declaration is as follows:

name [ CONSTANT ] type [ COLLATE collation_name ] [ NOT NULL ] [ { DEFAULT | := | = } expression ];
  • name: The name should follow the naming rules discussed in Chapter 03, PostgreSQL Basic Building Blocks. For example, the name should not start with an integer.
  • CONSTANT: The variable cannot be assigned another value after the initialization. This is useful in defining constant variables such as PI.
  • type: The type of variable can be simple, such as an integer, user-defined data type, pseudo type, record, and so on. Since a type is created implicitly when creating a table, one can use this type to declare a variable.
In PostgreSQL, the following two declarations are equivalent; however, the second declaration is more portable with Oracle. Additionally, it is more descriptive of the intent; and it can not be confused as a reference to the actual table. This is the preferred type declaration style.
  • myrow tablename;
  • myrow tablename%ROWTYPE;
  • NOT NULL: NOT NULL causes a runtime error to be raised if the variable is assigned a null. NOT NULL variables must be initialized.
  • DEFAULT: Causes the initialization of the variable to be delayed until the block is entered. This is useful in defining a timestamp variable to indicate when the function is called, but not the function precompilation time.
  • An expression is a combination of one or more explicit values, operators, and functions that can be evaluated to another value.

The PostgreSQL PL/pgSQL function body is composed of nested blocks with an optional declaration section and a label. Variables are declared in the declare section of the block, shown as follows:

[ <<label>> ]
[ DECLARE
declarations ]
BEGIN
statements
END [ label ];

The BEGIN and END keywords are not used in this context to control transactional behavior, but only for grouping. The declaration section is used for declaring variables, and the label is used to give a name to the block, as well as to give fully qualified names to the variables. Finally, in each PL/pgSQL function, there is a hidden block labeled with a function name that contains predefined variables, such as FOUND. To understand the function block, let's take a look at the following code, which defines the factorial function in a recursive manner:

CREATE OR REPLACE FUNCTION factorial(INTEGER ) RETURNS INTEGER AS $$
BEGIN
IF $1 IS NULL OR $1 < 0 THEN RAISE NOTICE 'Invalid Number';
RETURN NULL;
ELSIF $1 = 1 THEN
RETURN 1;
ELSE
RETURN factorial($1 - 1) * $1;
END IF;
END;
$$ LANGUAGE 'plpgsql';

The block defines the variable scope; in our example, the scope of the argument variable $1 is the whole function. Also, as shown in the example, there is no declaration section. To understand the scope between different code blocks, let's write the factorial function in a slightly different manner, which is as follows:

CREATE OR REPLACE FUNCTION factorial(INTEGER ) RETURNS INTEGER AS $$
DECLARE
fact ALIAS FOR $1;
BEGIN
IF fact IS NULL OR fact < 0 THEN
RAISE NOTICE 'Invalid Number';
RETURN NULL;
ELSIF fact = 1 THEN
RETURN 1;
END IF;
DECLARE
result INT;
BEGIN
result = factorial(fact - 1) * fact;
RETURN result;
END;
END;
$$ LANGUAGE 'plpgsql';

The preceding function is composed of two blocks: the variable fact is an alias for the first argument. In the subblock, the result variable is declared with a type integer. Since the fact variable is defined in the upper block, it can also be used in the subblock. The result variable can be used only in the subblock.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset