A newer version of this documentation is available.

View Latest

Calling a User-Defined Function

  • how-to
    +
    Calling a User-Defined Function from N1QL statements.

    Introduction

    A User-Defined Function can be called like any other N1QL function. The Javascript is not called directly; it is called through the N1QL User-Defined Function.

    Calling the N1QL User-Defined Function

    A N1QL User-Defined Function can be called from anywhere that a standard N1QL function can be called.

    • Query Workbench

    • REST API

    • N1QL

    1. Access the Query Workbench UI from the Administration Console.

      accessing the query tool
    2. Set the context to match the namespace of the function you are calling.

      switch context to travel sample
    3. Enter the N1QL statement in the query editor to run your function:

      EXECUTE FUNCTION GetBusinessDays("02/14/2022", "04/16/2022");
    1. Open up a shell session.

    2. Execute a curl command to run the function:

      curl -v http://localhost:8093/query/service \
        -u Administrator:password \
        -d 'statement=EXECUTE FUNCTION default:`travel-sample`.inventory.GetBusinessDays("03/10/2022", "05/10.2022")'

    Run the EXECUTE FUNCTION function in the CBQ Shell.

    EXECUTE FUNCTION default:`travel-sample`.`inventory`.GetBusinessDays("03/10/2022", "05/10.2022");

    The N1QL User-Defined Function can be used in any N1QL statement in exactly the same way as a standard built-in function.

    SELECT CASE 
      WHEN  GetBusinessDays('02/14/2022', '4/16/2022') > 44 THEN "true" 
      ELSE "false" 
      END 
      AS response;