TechTip: Create Your Own SQL User-Defined Functions (UDFs)

SQL
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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:

http://www.mcpressonline.com/articles/images/2002/SQL%20WizardV4--Forstythe--05100400.png

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:

http://www.mcpressonline.com/articles/images/2002/SQL%20WizardV4--Forstythe--05100401.png

Figure 2: Start by entering some general information about your function.

http://www.mcpressonline.com/articles/images/2002/SQL%20WizardV4--Forstythe--05100402.png

Figure 3: Set your parameters.

http://www.mcpressonline.com/articles/images/2002/SQL%20WizardV4--Forstythe--05100403.png

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.





BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: