User‑Defined Functions and Large Objects
The usage characteristics for a user‑defined function with large object parameters or a large object return value are the same as the usage characteristics for any other UDF. In general, you can specify a UDF that accepts a LOB value as an argument in any context in which a UDF is otherwise allowed. You can also use a UDF that returns a LOB value in any context in which a value of that type is appropriate and a UDF is otherwise allowed.
As with other functions and operators, automatic type conversions can be applied to the arguments or the return values of a UDF. You should be careful about the possible performance implications of automatic type conversions with large objects used as UDF parameters. For example, a function whose formal parameter is a BLOB type could be passed a VARBYTE column as the actual parameter. The system converts the VARBYTE value into a temporary BLOB and then passes that to the UDF. Because even a temporary BLOB is stored on disk, the performance cost of the conversion is significant.
To avoid this, you should consider creating an overloaded function that explicitly accepts a VARBYTE TD_ANYTYPE argument. Note that you cannot create overloaded functions to enforce row‑level security constraints.
Another possible cause of undesired conversions is truncation. Declared length is part of the data type. Consider a UDF whose formal parameter is declared to be BLOB(100000) AS LOCATOR, and suppose the actual parameter is a column whose type is declared as BLOB without a locator specification. In this case, the maximum length of the source type defaults to 2,097,088,000 bytes. Whenever the source type is longer than the target type, truncation might occur. Because of this, an automatic conversion operation, which results in a data copy as well as the creation of a temporary BLOB, must be generated. Therefore, whenever possible, you should define UDFs to accept the maximum length object by omitting the length specification.
This rule contradicts the policy for all other data types, which is that data type length declarations should only be as long as is necessary. See SQL External Routine Programming for details.