<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>OpenText Analytics Database 26.2.x – User-defined SQL functions</title>
    <link>/en/extending/user-defined-sql-functions/</link>
    <description>Recent content in User-defined SQL functions on OpenText Analytics Database 26.2.x</description>
    <generator>Hugo -- gohugo.io</generator>
    
	  <atom:link href="/en/extending/user-defined-sql-functions/index.xml" rel="self" type="application/rss+xml" />
    
    
      
        
      
    
    
    <item>
      <title>Extending: Creating user-defined SQL functions</title>
      <link>/en/extending/user-defined-sql-functions/creating-user-defined-sql-functions/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/extending/user-defined-sql-functions/creating-user-defined-sql-functions/</guid>
      <description>
        
        
        &lt;p&gt;A user-defined SQL function can be used anywhere in a query where an ordinary SQL expression can be used, except in the table partition clause or the projection segmentation clause.&lt;/p&gt;
&lt;p&gt;To create a SQL function, the user must have CREATE privileges on the schema. To use a SQL function, the user must have USAGE privileges on the schema and EXECUTE privileges on the defined function.&lt;/p&gt;
&lt;p&gt;This following statement creates a SQL function called &lt;code&gt;myzeroifnull&lt;/code&gt; that accepts an &lt;code&gt;INTEGER&lt;/code&gt; argument and returns an &lt;code&gt;INTEGER&lt;/code&gt; result.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE FUNCTION myzeroifnull(x INT) RETURN INT
   AS BEGIN
     RETURN (CASE WHEN (x IS NOT NULL) THEN x ELSE 0 END);
   END;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can use the new SQL function (&lt;code&gt;myzeroifnull&lt;/code&gt;) anywhere you use an ordinary SQL expression. For example, create a simple table:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TABLE tabwnulls(col1 INT);
=&amp;gt; INSERT INTO tabwnulls VALUES(1);
=&amp;gt; INSERT INTO tabwnulls VALUES(NULL);
=&amp;gt; INSERT INTO tabwnulls VALUES(0);
=&amp;gt; SELECT * FROM tabwnulls;
 a
---
 1
 0
(3 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Use the &lt;code&gt;myzeroifnull&lt;/code&gt; function in a &lt;code&gt;SELECT&lt;/code&gt; statement, where the function calls &lt;code&gt;col1&lt;/code&gt; from table tabwnulls:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT myzeroifnull(col1) FROM tabwnulls;
 myzeroifnull
--------------
          1
          0
          0
(3 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Use the &lt;code&gt;myzeroifnull&lt;/code&gt; function in the &lt;code&gt;GROUP BY&lt;/code&gt; clause:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT COUNT(*) FROM tabwnulls GROUP BY myzeroifnull(col1);
 count
-------
     2
     1
(2 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If you want to change a user-defined SQL function&#39;s body, use the &lt;code&gt;CREATE OR REPLACE&lt;/code&gt; syntax. The following command modifies the CASE expression:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE OR REPLACE FUNCTION myzeroifnull(x INT) RETURN INT
   AS BEGIN
     RETURN (CASE WHEN (x IS NULL) THEN 0 ELSE x END);
   END;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To see how this information is stored in the OpenText™ Analytics Database catalog, see &lt;a href=&#34;../../../en/extending/user-defined-sql-functions/viewing-information-about-user-defined-sql-functions/&#34;&gt;Viewing Information About SQL Functions&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;see-also&#34;&gt;See also&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&#34;../../../en/sql-reference/statements/create-statements/create-function-statements/create-function-sql/#&#34;&gt;CREATE FUNCTION (SQL)&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../en/sql-reference/system-tables/v-catalog-schema/user-functions/#&#34;&gt;USER_FUNCTIONS&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
    <item>
      <title>Extending: Altering and dropping user-defined SQL functions</title>
      <link>/en/extending/user-defined-sql-functions/altering-and-dropping-user-defined-sql-functions/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/extending/user-defined-sql-functions/altering-and-dropping-user-defined-sql-functions/</guid>
      <description>
        
        
        &lt;p&gt;OpenText™ Analytics Database allows multiple functions to share the same name with different argument types. Therefore, if you try to alter or drop a SQL function without specifying the argument data type, the system returns an error message to prevent you from dropping the wrong function:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; DROP FUNCTION myzeroifnull();
ROLLBACK:  Function with specified name and parameters does not exist: myzeroifnull
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&#34;alert admonition note&#34; role=&#34;alert&#34;&gt;
&lt;h4 class=&#34;admonition-head&#34;&gt;Note&lt;/h4&gt;

Only a superuser or owner can alter or drop a SQL Function.

&lt;/div&gt;
&lt;h2 id=&#34;altering-a-user-defined-sql-function&#34;&gt;Altering a user-defined SQL function&lt;/h2&gt;
&lt;p&gt;The &lt;a href=&#34;../../../en/sql-reference/statements/alter-statements/alter-function-statements/alter-function-scalar/#&#34;&gt;ALTER FUNCTION (scalar)&lt;/a&gt; command lets you assign a new name to a user-defined function, as well as move it to a different schema.&lt;/p&gt;
&lt;p&gt;In the previous topic, you created a SQL function called &lt;code&gt;myzeroifnull&lt;/code&gt;. The following command renames the &lt;code&gt;myzeroifnull&lt;/code&gt; function to &lt;code&gt;zerowhennull&lt;/code&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER FUNCTION myzeroifnull(x INT) RENAME TO zerowhennull;
ALTER FUNCTION
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This next command moves the renamed function into a new schema called &lt;code&gt;macros&lt;/code&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER FUNCTION zerowhennull(x INT) SET SCHEMA macros;
ALTER FUNCTION
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;dropping-a-sql-function&#34;&gt;Dropping a SQL function&lt;/h2&gt;
&lt;p&gt;The &lt;a href=&#34;../../../en/sql-reference/statements/drop-statements/drop-function/#&#34;&gt;DROP FUNCTION&lt;/a&gt; command drops a SQL function from the database catalog.&lt;/p&gt;
&lt;p&gt;Like with ALTER FUNCTION, you must specify the argument data type or the system returns the following error message:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; DROP FUNCTION zerowhennull();
ROLLBACK:  Function with specified name and parameters does not exist: zerowhennull
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Specify the argument type:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; DROP FUNCTION macros.zerowhennull(x INT);
DROP FUNCTION
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The database does not check for dependencies, so if you drop a SQL function where other objects reference it (such as views or other SQL Functions), the database returns an error when those objects are used, not when the function is dropped.

&lt;div class=&#34;alert admonition tip&#34; role=&#34;alert&#34;&gt;
&lt;h4 class=&#34;admonition-head&#34;&gt;Tip&lt;/h4&gt;

To view a list of all user-defined SQL functions on which you have EXECUTE privileges, (which also returns their argument types), query the &lt;a href=&#34;../../../en/sql-reference/system-tables/v-catalog-schema/user-functions/&#34;&gt;V_CATALOG.USER_FUNCTIONS&lt;/a&gt; system table.

&lt;/div&gt;&lt;/p&gt;
&lt;h2 id=&#34;see-also&#34;&gt;See also&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&#34;../../../en/sql-reference/statements/alter-statements/alter-function-statements/alter-function-scalar/#&#34;&gt;ALTER FUNCTION (scalar)&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../en/sql-reference/statements/drop-statements/drop-function/#&#34;&gt;DROP FUNCTION&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
    <item>
      <title>Extending: Managing access to SQL functions</title>
      <link>/en/extending/user-defined-sql-functions/managing-access-to-sql-functions/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/extending/user-defined-sql-functions/managing-access-to-sql-functions/</guid>
      <description>
        
        
        &lt;p&gt;Before a user can execute a user-defined SQL function, he or she must have USAGE privileges on the schema and EXECUTE privileges on the defined function. Only the superuser or owner can grant/revoke EXECUTE usage on a function.&lt;/p&gt;
&lt;p&gt;To grant EXECUTE privileges to user Fred on the &lt;code&gt;myzeroifnull&lt;/code&gt; function:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; GRANT EXECUTE ON FUNCTION myzeroifnull (x INT) TO Fred;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To revoke EXECUTE privileges from user Fred on the &lt;code&gt;myzeroifnull&lt;/code&gt; function:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; REVOKE EXECUTE ON FUNCTION myzeroifnull (x INT) FROM Fred;
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;see-also&#34;&gt;See also&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&#34;../../../en/sql-reference/statements/grant-statements/grant-user-defined-extension/#&#34;&gt;GRANT (user defined extension)&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../en/sql-reference/statements/revoke-statements/revoke-user-defined-extension/#&#34;&gt;REVOKE (user defined extension)&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
    <item>
      <title>Extending: Viewing information about user-defined SQL functions</title>
      <link>/en/extending/user-defined-sql-functions/viewing-information-about-user-defined-sql-functions/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/extending/user-defined-sql-functions/viewing-information-about-user-defined-sql-functions/</guid>
      <description>
        
        
        &lt;p&gt;You can access information about user-defined SQL functions on which you have EXECUTE privileges. This information is available in system table &lt;a href=&#34;../../../en/sql-reference/system-tables/v-catalog-schema/user-functions/#&#34;&gt;USER_FUNCTIONS&lt;/a&gt; and from the vsql meta-command &lt;code&gt;\df&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To view all user-defined SQL functions on which you have EXECUTE privileges, query USER_FUNCTIONS:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM USER_FUNCTIONS;
-[ RECORD 1 ]----------+---------------------------------------------------
schema_name            | public
function_name          | myzeroifnull
function_return_type   | Integer
function_argument_type | x Integer
function_definition    | RETURN CASE WHEN (x IS NOT NULL) THEN x ELSE 0 END
volatility             | immutable
is_strict              | f
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If you want to change the body of a user-defined SQL function, use the CREATE OR REPLACE syntax. The following command modifies the CASE expression:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE OR REPLACE FUNCTION myzeroifnull(x INT) RETURN INT
   AS BEGIN
     RETURN (CASE WHEN (x IS NULL) THEN 0 ELSE x END);
   END;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now when you query USER_FUNCTIONS, you can see the changes in the &lt;code&gt;function_definition&lt;/code&gt; column:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM USER_FUNCTIONS;
-[ RECORD 1 ]----------+---------------------------------------------------
schema_name            | public
function_name          | myzeroifnull
function_return_type   | Integer
function_argument_type | x Integer
function_definition    | RETURN CASE WHEN (x IS NULL) THEN 0 ELSE x END
volatility             | immutable
is_strict              | f
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If you use CREATE OR REPLACE syntax to change only the argument name or argument type (or both), the system maintains both versions of the function. For example, the following command tells the function to accept and return a numeric data type instead of an integer for the &lt;code&gt;myzeroifnull&lt;/code&gt; function:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE OR REPLACE FUNCTION myzeroifnull(z NUMERIC) RETURN NUMERIC
   AS BEGIN
     RETURN (CASE WHEN (z IS NULL) THEN 0 ELSE z END);
   END;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now query the USER_FUNCTIONS table, and you can see the second instance of &lt;code&gt;myzeroifnull&lt;/code&gt; in Record 2, as well as the changes in the &lt;code&gt;function_return_type&lt;/code&gt;, &lt;code&gt;function_argument_type&lt;/code&gt;, and &lt;code&gt;function_definition&lt;/code&gt; columns.

&lt;div class=&#34;alert admonition note&#34; role=&#34;alert&#34;&gt;
&lt;h4 class=&#34;admonition-head&#34;&gt;Note&lt;/h4&gt;

Record 1 still holds the original definition for the &lt;code&gt;myzeroifnull&lt;/code&gt; function:

&lt;/div&gt;&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM USER_FUNCTIONS;
-[ RECORD 1 ]----------+------------------------------------------------------------
schema_name            | public
function_name          | myzeroifnull
function_return_type   | Integer
function_argument_type | x Integer
function_definition    | RETURN CASE WHEN (x IS NULL) THEN 0 ELSE x END
volatility             | immutable
is_strict              | f
-[ RECORD 2 ]----------+------------------------------------------------------------
schema_name            | public
function_name          | myzeroifnull
function_return_type   | Numeric
function_argument_type | z Numeric
function_definition    | RETURN (CASE WHEN (z IS NULL) THEN (0) ELSE z END)::numeric
volatility             | immutable
is_strict              | f
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Because OpenText™ Analytics Database allows functions to share the same name with different argument types, you must specify the argument type when you &lt;a href=&#34;../../../en/sql-reference/statements/alter-statements/alter-function-statements/alter-function-scalar/&#34;&gt;alter&lt;/a&gt; or &lt;a href=&#34;../../../en/sql-reference/statements/drop-statements/drop-function/&#34;&gt;drop&lt;/a&gt; a function. If you do not, the system returns an error message:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; DROP FUNCTION myzeroifnull();
ROLLBACK:  Function with specified name and parameters does not exist: myzeroifnull
&lt;/code&gt;&lt;/pre&gt;
      </description>
    </item>
    
    <item>
      <title>Extending: Migrating built-in SQL functions</title>
      <link>/en/extending/user-defined-sql-functions/migrating-built-sql-functions/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/extending/user-defined-sql-functions/migrating-built-sql-functions/</guid>
      <description>
        
        
        &lt;p&gt;If you have built-in SQL functions from another RDBMS that do not map to a OpenText™ Analytics Database-supported function, you can migrate them into your database by using a user-defined SQL function.&lt;/p&gt;
&lt;p&gt;The example scripts below show how to create user-defined functions for the following DB2 built-in functions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;UCASE()&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;LCASE()&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;LOCATE()&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;POSSTR()&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;ucase&#34;&gt;UCASE()&lt;/h2&gt;
&lt;p&gt;This script creates a user-defined SQL function for the &lt;code&gt;UCASE()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE OR REPLACE FUNCTION UCASE (x VARCHAR)
   RETURN VARCHAR
   AS BEGIN
   RETURN UPPER(x);
   END;
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;lcase&#34;&gt;LCASE()&lt;/h2&gt;
&lt;p&gt;This script creates a user-defined SQL function for the &lt;code&gt;LCASE()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE OR REPLACE FUNCTION LCASE (x VARCHAR)
   RETURN VARCHAR
   AS BEGIN
   RETURN LOWER(x);
   END;
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;locate&#34;&gt;LOCATE()&lt;/h2&gt;
&lt;p&gt;This script creates a user-defined SQL function for the &lt;code&gt;LOCATE()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE OR REPLACE FUNCTION LOCATE(a VARCHAR, b VARCHAR)
   RETURN INT
   AS BEGIN
   RETURN POSITION(a IN b);
   END;
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;posstr&#34;&gt;POSSTR()&lt;/h2&gt;
&lt;p&gt;This script creates a user-defined SQL function for the &lt;code&gt;POSSTR()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE OR REPLACE FUNCTION POSSTR(a VARCHAR, b VARCHAR)
   RETURN INT
   AS BEGIN
   RETURN POSITION(b IN a);
   END;
&lt;/code&gt;&lt;/pre&gt;
      </description>
    </item>
    
  </channel>
</rss>
