SQL contains a wide variety of built-in functions that provide great tools to SQL users. But most applications have certain situations that could benefit from more-unique functions that are designed to interact with that application and its specific database requirements. Every database has some column where the data is stored in an unusual format or where some complex logic is required to extract the data.
For just these situations, SQL includes the ability to extend itself. Developers can create customized user-defined functions (UDFs) to perform tasks that are specific to their database.
There are three types of UDFs: external, sourced, and SQL.
External functions are written in some language other than SQL, such as RPG IV, and SQL merely maps its UDF to a subprocedure in a given service program. These are easier to code for programmers who are already comfortable with high-level languages (HLLs).
Sourced functions are simply wrappers around other functions. They translate the data types of the parameters from one type into the appropriate data type for the target function.
SQL functions are written with SQL syntax. This type of function has greater cross-platform portability, as it's not tied to a HLL program on the server.
To create an SQL function using the new wizard in Operations Navigator, drill down into the database application as shown in Figure 1:
Figure 1: Create your own UDF with the Operations Navigator wizard. (Click images to enlarge.)
Selecting an SQL function launches the function wizard, which walks you through the process of creating a function. Fill out each panel of the wizard as shown in Figures 2, 3, and 4:
Figure 4: Enter your SQL statements.
This example function combines month, day, and year fields into a formatted date (mm/dd/yyyy). It can be invoked as shown here:
SELECT GETDATE(MYMNTH,MYDAY,MYYEAR) FROM MYFILE
This is just a simple example to introduce you to the tool. If you haven't started using this wizard yet, give it a try. It's one of many great features within iSeries Navigator.
Kevin Forsythe has over 18 years of experience working with the iSeries platform and its predecessors. He has been a member of the DMC team for the past nine years. Kevin's primary responsibility is providing iSeries education, but he also provides customers with project management, system design, analysis, and technical construction. In addition to his technical skills (RPG IV, CL, OS/400, SQL, FTP, Query, VB, Net.Data), Kevin possesses the ability to communicate new and complex concepts to his students. He has been the primary instructor for DMC's iSeries-based AS/Credentials training courses since 1997 and has authored courses such as Advanced ILE, SQL, Embedded SQL, Operations Navigator, and Intro to WebSphere Studio. An award-winning speaker, he has spoken at every COMMON Conference since the spring of 2000.