Creating procedures and functions with external calls

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

For a full description of the CREATE PROCEDURE statement syntax, see “CREATE PROCEDURE statement” in Sybase IQ Reference Manual.

For a full description of the CREATE FUNCTION statement syntax for external calls, see “CREATE FUNCTION statement” in Sybase IQ Reference Manual.

NoteYou 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 DLL library.dll as follows:

CREATE PROCEDURE dll_proc ( parameter-list )
EXTERNAL NAME 'function_name@library.dll'

Such a procedure is called an external stored procedure. If you call an external DLL from a procedure, the procedure cannot carry out any other tasks; it just forms a wrapper around the DLL.

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 dynamic link library, 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 argument using an API described in “External function prototypes”.

Any value returned by the external function is in turn returned by the procedure to the calling environment.

No other statements permitted

A procedure that references an external function can include no other statements: its sole purposes are 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.

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. For example:

CREATE PROCEDURE dll_proc ( parameter-list )
EXTERNAL NAME 'UNIX:UX_fn@UX_lib.dll;WindowsNT:nt_fn@nt_lib.dll'

The operating system identifier must be Windows or UNIX.

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.