Declares a routine interface for use in database definition
statements. A routine is either a function or a procedure.
The declared routine acts as a template for calls to the function
or procedure in DDL statements such as CREATE TABLE, CREATE VIEW
and CREATE MODULE. The template allows Rdb to validate that
the routine is correctly named, is passed the correct number
of parameters and that those parameters are passed compatible
arguments. For functions the returned data type is used to
calculate data types for COMPUTED BY, AUTOMATIC and other stored
value expressions.
1 – Environment
You can use the DECLARE Routine statement:
o In interactive SQL
o In dynamic SQL as a statement to be dynamically executed
2 – Format
(B)0[m[1;4mDECLARE[m [1mqqwq> [1;4mFUNCTION[m[1m qqqwq> <routine-name> qqqqqqqqqqqqqqqqqqk [m [1m [m [1m mq> [1;4mPROCEDURE[m[1m qqj x [m [1m lqqqqqqqqqqqqqqqqqqqqqqqqqqqqq <qqqqqqqqqqqqqqqqqqqqqqqqqqqqqj [m [1m mqwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqqqqqqqk [m [1m mq> [1;4mSTORED[m[1m [1;4mNAME[m[1m IS <identifier> qj [m [1m x [m [1m lqqqqqqqqqqqqqqqqqqqqqqqqqqq[m [1m<qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj [m [1m mq> ( qwqqqqqqqqqqqqqqqqqqqqqqqwq> ) qqqqqqqqqqqqqqqqqqqqqqqqk [m [1m mwq> parameter-list qqwqj [m [1m x [m [1m mqqqqqqqq , <qqqqqqqqj [m [1m x [m [1m lqqqqqqqqqqqqqqqqqqqqqqqqqqq[m [1m<qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj [m [1m mqwqqqqqqqqqqqqqqqqqqqwqq> qqwqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqqqq> [m [1m mq> returns-clause qj mq> [1;4mLANGUAGE[m[1m [1;4mSQL[m[1m qqj [m [1m [m (B)0[m[1mparameter-list = [m [1m [m [1mqwqqqqqqqqqqwqwqqqqqqqqqqqqqqqqqqqwqwq> data-type qqqqqwqqqqqqqqqqk [m [1m tq> [1;4mIN[m[1m qqqqu m> <parameter-name> j mq> <domain-name> qj [m [1mx [m [1m tq> [1;4mOUT[m[1m qqqu [m [1mx[m [1m mq> [1;4mINOUT[m[1m qj [m [1mx[m [1mlqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj[m [1mmqwqqqqqqqqqqqqqqqqqqqqqqqqqqqwqqwqqqqqqqqqqqqqqqqqqqqqwqqqqqqqqqqk [m [1m mq> [1;4mDEFAULT[m[1m value-expr qqqqqj[m [1mmq> mechanism-clause qj x [m [1m lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq<qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj [m [1m mwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq>[m [1m mq> [1;4mCOMMENT[m[1m [1;4mIS[m[1m qwq> 'string' qqqwqj [m [1m mqqqqqq / <qqqqqj [m (B)0[m[1mmechanism-clause = [m [1m [m [1mqqqq> [1;4mBY[m[1m qqwq> [1;4mDESCRIPTOR[m[1m qwqqq> [m [1m tq> [1;4mLENGTH[m[1m qqqqqu [m [1m tq> [1;4mREFERENCE[m[1m qqu [m [1m mq> [1;4mVALUE[m[1m qqqqqqj [m [1m [m (B)0[m[1mreturns-clause = [m [1m [m [1m qqq> [1;4mRETURNS[m[1m qw> result-data-type qwwqqqqqqqqqqqqqqqqqqqqqwqq> [m [1m m> <domain-name> qqqqjmq> mechanism-clause qj [m
3 – Arguments
3.1 – DEFAULT value-expr
Specifies the default value of a parameter for a function or
procedure defined with mode IN. If you omit this parameter or if
the CALL statement argument list or function invocation specifies
the DEFAULT keyword, then the value-expr specified with this
clause is used. The parameter uses NULL as the default if you do
not specify a value expression explicitly.
3.2 – FUNCTION
Declares a function definition.
A function optionally accepts a list of IN parameters, always
returns a value, and is referenced by name as an element of a
value expression.
3.3 – LANGUAGE_SQL
Names the language that calls the routine.
3.4 – mechanism-clause
Defines the passing mechanism for an external routine. The
following list describes the passing mechanisms.
o BY DESCRIPTOR
Allows passing character data with any parameter access mode
to routines compiled by language compilers that implement the
OpenVMS calling standard.
o BY LENGTH
The LENGTH passing mechanism is the same as the DESCRIPTOR
passing mechanism.
o BY REFERENCE
Allows passing data with any parameter access mode as a
reference to the actual data.
This is the default passing mechanism for parameters. This
is also the default passing mechanism for a function value
returning character data.
o BY VALUE
Allows passing data with the IN parameter access mode to a
routine as a value and allows functions to return a value.
This is the default passing mechanism for a function value
returning noncharacter data.
3.5 – parameter-list
The optional parameters of the routine. For each parameter
you can specify a parameter access mode (IN, OUT, and INOUT),
a parameter name, a data type, and a passing mechanism (by
DESCRIPTOR, LENGTH, REFERENCE, or VALUE).
The parameter access mode (IN, OUT, and INOUT) is optional and
specifies how the parameter is accessed (whether it is read,
written, or both). IN signifies read only, OUT signifies write
only, and INOUT signifies read and write. The parameter access
mode defaults to IN.
Only the IN parameter access mode may be specified with
parameters to a function. Any of the parameter access modes (IN,
OUT, and INOUT) may be specified with parameters to a procedure.
The parameter name is prefixed with a colon (:). The parameter
name must be unique within the routine parameters.
The data type is required and describes the type of parameter
using either an SQL data type or a domain name.
You cannot declare a parameter as the LIST OF BYTE VARYING data
type.
3.6 – PROCEDURE
Declares a procedure definition.
A procedure optionally accepts a list of IN, OUT, or INOUT
parameters, and is referenced by name in a CALL statement.
3.7 – RETURNS
Describes a function (returned) value. You can specify a data
type and a passing mechanism (BY DESCRIPTOR, LENGTH, REFERENCE,
or VALUE). The function value is, by definition, an OUT access
mode value.
The data type is required and describes the type of parameter
using either an SQL data type or a domain name.
You cannot declare a function value as the LIST OF BYTE VARYING
data type.
3.8 – routine-name
The name of the external routine. The name must be unique among
external and stored routines in the schema and can be qualified
with an alias or, in a multischema database, a schema name.
3.9 – STORED_NAME_IS
The name that Oracle Rdb uses to access the routine when defined
in a multischema database. The stored name allows you to access
multischema definitions using interfaces that do not recognize
multiple schemas in one database. You cannot specify a stored
name for a routine in a database that does not allow multiple
schemas. For more information about stored names, see Stored
Names.
4 – Examples
Example 1: Definining a domain and referencing an external
function
SQL> create domain MONEY as integer (2);
SQL>
SQL> create function INTEREST_PAID
cont> (in :amt MONEY)
cont> returns MONEY;
cont> external
cont> language C
cont> parameter style GENERAL;
SQL>
SQL> alter domain MONEY
cont> add
cont> check (INTEREST_PAID (value) > 0)
cont> not deferrable;
Once the ALTER DOMAIN is completed, neither the function nor the
domain can be defined before the other. Here is a fragment of the
result of executing the output from the RMU Extract command.
SQL> create domain MONEY
cont> INTEGER (2)
cont> check((INTEREST_PAID(value) > 0))
cont> not deferrable;
%SQL-F-RTNNOTDEF, function or procedure INTEREST_PAID is not defined
SQL>
SQL> commit work;
SQL> create function INTEREST_PAID (
cont> in :AMT
cont> MONEY
cont> by reference)
cont> returns
cont> MONEY by value
cont> language SQL;
cont> external
cont> language C
cont> parameter style GENERAL
cont> deterministic
cont> called on null input
cont> ;
%SQL-F-NO_SUCH_FIELD, Domain MONEY does not exist in this database or schema
SQL> commit work;
This problem is avoided for RMU Extract by adding the FORWARD_
REFERENCES item to the command line:
$ RMU/EXTRACT/ITEM=(ALL,FORWARD_REFERENCES) databasename/OUTPUT=script.SQL
The script now contains a forward declaration of the function
INTEREST_PAID so that execution of the script can succeed.
SQL> declare function INTEREST_PAID (
cont> in :AMT
cont> INTEGER (2))
cont> returns
cont> INTEGER (2)
cont> ;
SQL>
SQL> create domain MONEY
cont> INTEGER (2)
cont> check((INTEREST_PAID(value) > 0))
cont> not deferrable;
SQL>
SQL> commit work;
SQL> create function INTEREST_PAID (
cont> in :AMT
cont> MONEY
cont> by reference)
cont> returns
cont> MONEY by value
cont> language SQL;
cont> external
cont> language C
cont> parameter style GENERAL
cont> deterministic
cont> called on null input
cont> ;
SQL> commit work;