16.10.2015 Views

Getting Started with IBM Data Studio for DB2

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

265<br />

Chapter 9 – Developing user-defined functions<br />

In this chapter, you will learn how to develop user-defined functions (UDFs) by using <strong>Data</strong><br />

<strong>Studio</strong>.<br />

9.1 User-defined functions: The big picture<br />

Like stored procedures, UDFs encapsulate reusable code. Because UDFs can be used in<br />

SQL statements, they let you extend the SQL language <strong>with</strong> your own logic. For example,<br />

you might want to create a function that encapsulates the logic to calculate a tax value in<br />

your country when a source value is given as input, or you can create a function that<br />

extracts in<strong>for</strong>mation from an XML document and returns it in a tabular <strong>for</strong>m that can be<br />

used to per<strong>for</strong>m a JOIN <strong>with</strong> another table. UDFs provide a very flexible way to extend your<br />

application.<br />

The supported languages <strong>for</strong> UDFs that are developed using <strong>Data</strong> <strong>Studio</strong> are: SQL, and<br />

PL/SQL. The <strong>DB2</strong> server supports other languages <strong>for</strong> UDFs, such as Java and C++, but<br />

those are not supported in <strong>Data</strong> <strong>Studio</strong>.<br />

Note:<br />

Although you can use <strong>Data</strong> <strong>Studio</strong> to create PL/SQL functions <strong>for</strong> <strong>DB2</strong> projects, PL/SQL<br />

support is not available in <strong>DB2</strong> Express-C, which is the edition of <strong>DB2</strong> used <strong>for</strong> the<br />

examples in this book. If you wish to develop PL/SQL functions, you’ll need to upgrade<br />

to a different edition of <strong>DB2</strong> that contains PL/SQL support.<br />

UDFs developed in <strong>Data</strong> <strong>Studio</strong> can have one of the following return types:<br />

<br />

<br />

Scalar: UDFs that accept one or more scalar values as input parameters and return a<br />

scalar value as result. Examples of such functions include the built-in length(), and<br />

concat() functions.<br />

Table: UDFs that accept individual scalar values as parameters and return a table to<br />

the SQL statement which references it. You can reference table functions in the FROM<br />

clause of a SELECT SQL statement.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!