The tag execute-function allows to perform the call to a function compiled and stored in the database and to obtain the result which returns.

# 1 Type of return: automatic / forced

The mode to perform the call to a SQL function, depends of the database agent. The tag execute-function has an optional attribute return-type or return-like which indicates the data type of the result, the XSQL-Script interpret will try to perform the conversion of the value returned to the indicated type. This is important because depending of the used database agent and the way in which the call is made it is not always possible to obtain the type of data that the SQL function returns. For this it is recommended to force its type and make the program independent of the database agent.

 JDBC call to a SQL function. Attribute [type] Agent Mode [type] required IDS EXECUTE FUNCTION No DB2 SELECT No ORACLE CALL Yes MYSQL SELECT No SQLSELECT CALL Yes POSTGRES SELECT No

For IDS, DB2 and MYSQL is not necessary to indicate the returned data type because the call to the function is performed through a cursor JDBC which allows to obtain the data type automatically. Otherwise in ORACLE and SQLSERVER the call is perfomed through a CALL which requires to indicate the data type returned to force the conversion. For this it is recommended to always indicate the type and thus the program is independent of the agent. The following example shows a function which returns a decimal value. This function is written in grammar XSQL, has been converted and compiled in the database.

Copy
<function name='get_test_decimal'>
<args>
<arg name='p_fecha' type='date' />
</args>
<returns type='decimal' size='10,6' />
<define>
<variable name='v_test' type='decimal' size='10,6' />
</define>
<body>
<set name='v_test'>1.789</set>
<return>v_test</return>
</body>
</function>

The following XSQL-Script performs a call to the function described previously.

Copy
<xsql-script name='script_test_decimal'>
<body>
<set name='m_cambio' type='decimal'>
<execute-function name='get_test_decimal' return-type='decimal'>
<date.today />
</execute-function>
</set>
</body>
</xsql-script>

This XSQL-Script will work well in IDS, DB2 and MYSQL, however in ORACLE and SQLSERVER will causes an exception:

Copy
unsupported conversion from data=1,789 of class=java.lang.String to type=decimal

Being necessary to indicate the data type returned in the call to the function

Copy
<execute-function name='get_test_decimal' return-type='decimal'>
<date.today />
</execute-function>

# 2 Cachear una llamada a una función

The tag execute-function allows to cache a call to a function. This implies that the result returned by the function is stored in cache and the following calls with the same input arguments will obtain the same result (the cached result). It has the sense that the function within the execution block is considered invariant. It should be used when you consider that the function is invariant. The following call to the function is performed with cache of result.

Copy
<execute-function name='get_test_decimal' return-type='decimal' cache='true'>
<date.today />
</execute-function>

Executes an SQL function compiled in the database and obtains the returned value.

<execute-function    name='name'    cache='cache'    return-type='string|boolean|smallint|integer|bigint|float|double|decimal|date|time|timestamp|blob'    return-like='table.column'    db2vtable='db2vtable'>    <argument /> *</execute-function>

#### Exceptions

unsupported conversion from ...

The value of the function is assigned to a variables which types are no convertibles.

SQLException ...

Excepcions provoked during the execution of the function.

Example

Performs the call to the function SQL get_test_decimal passing as argument the current date.

Copy
<xsql-script name='script_test_decimal'>
<body>
<set name='m_change' type='decimal'>
<execute-function name='get_test_decimal' return-type='decimal'>
<date.today />
</execute-function>
</set>
</body>
</xsql-script>