Creating procedures and functions with external calls

This section presents some examples of procedures and functions with external calls.

DBA authority required

You must have DBA authority to create procedures or functions that reference external libraries. This requirement is more strict than the RESOURCE authority required for creating other procedures or functions.

Syntax

You can create a procedure that calls a function function-name in the Windows Dynamic Link Library library.dll as follows:

CREATE PROCEDURE dll_proc( parameter-list )
  EXTERNAL NAME 'function-name@library.dll';

When you define a stored procedure or function in this way, you are creating a bridge to the function in the external DLL. The stored procedure or function cannot perform any other tasks.

An analogous CREATE FUNCTION statement is as follows:

CREATE FUNCTION dll_func ( parameter-list )
  RETURNS data-type
  EXTERNAL NAME 'function-name@library.dll';

In these statements, function-name is the exported name of a function in the DLL, and library.dll is the name of the library. The arguments in parameter-list must correspond in type and order to the arguments expected by the library function. The library function accesses the procedure arguments using an API described in External function prototypes.

Any value returned by the external function can be returned by the stored function or procedure to the calling environment.

No other statements permitted

A stored procedure or function that references an external function can include no other statements: its sole purpose is to take arguments for a function, call the function, and return any value and returned arguments from the function to the calling environment. You can use IN, INOUT, or OUT parameters in the procedure call in the same way as for other procedures: the input values get passed to the external function, and any parameters modified by the function are returned to the calling environment in OUT or INOUT parameters or as the RETURNS result of the stored function.

System-dependent calls

You can specify operating-system dependent calls, so that a procedure calls one function when run on one operating system, and another function (presumably analogous) on another operating system. The syntax for such calls involves prefixing the function name with the operating system name. The operating system identifier must be Unix. An example follows.

CREATE FUNCTION func ( parameter-list )
  RETURNS data-type
  EXTERNAL NAME 'Unix:function-name@library.so;function-name@library.dll';

If the list of functions does not contain an entry for the operating system on which the server is running, but the list does contain an entry without an operating system specified, the database server calls the function in that entry.

See also