SQL Built-In Functions and Stored Procedures: The i5/iSeries Programmer's Guide - Softcover

Faust, Mike

 
9781583470541: SQL Built-In Functions and Stored Procedures: The i5/iSeries Programmer's Guide

Inhaltsangabe

Stored procedures and built-in functions can greatly expand the power and usefulness of SQL, but surprisingly these highly useful tools remain untapped by many programmers. For programmers who are new to the subject or those who want to expand on what they already know, this book provides a terrific introduction to the creation and use of built-in functions and stored procedures under OS/400 and i5/OS. But what are they?

Built-in functions are a part of practically every programming language and can be a big boost to productivity by identifying and isolating functions that need to be executed repeatedly. SQL makes particularly good use of its built-in functions. Stored procedures are programs that can be called from within SQL&;and other programming languages, such as RPG&;to process entire datasets. This book will put you on the fast track to using and writing both effectively.

After you learn the basics of these powerful tools, you?ll go on to explore some of the stored procedures and functions included with SQL on the iSeries as part of OS/400 and i5/OS. Author Mike Faust even explains the process of developing custom built-in functions for your own use and has included a stored procedure that can be used to create a crosstab or pivot table representation of supplied data.

This book is the tool you need to boost the power of your i5/OS SQL programming skills.

With SQL Built-In Functions and Stored Procedures, you will:

&; Discover how to perform routine maintenance functions, such as copying a file or rebuilding an existing table without losing the data in the existing table.

&; Learn how to build data files, modify a library list, and compile programs.

&; Practice using example programs that not only teach, but also make excellent additions
to any programmer's library.

&; Learn how to use 11 different functions, including functions to edit numeric values for print with commas, decimal points, and currency symbols; a function that returns the distance in miles between two ZIP codes; functions to convert date and currency to a text string and more.

&; Gain access to complete code on the Web for each function, which you can use directly in your own programs.

Die Inhaltsangabe kann sich auf eine andere Ausgabe dieses Titels beziehen.

Über die Autorin bzw. den Autor

Mike Faust is a former network administrator, software developer, and MIS Manager. He lives in Sanford, Florida.

Auszug. © Genehmigter Nachdruck. Alle Rechte vorbehalten.

SQL Built-In Functions and Stored Procedures

The i5/iSeries Programmer's Guide

By Mike Faust

MC Press

Copyright © 2005 Mike Faust
All rights reserved.
ISBN: 978-1-58347-054-1

Contents

Title Page,
Copyright Page,
Introduction,
1 - SQL on the iSeries,
2 - Built-In Functions,
3 - Creating Custom Stored Procedures,
4 - Creating User-Defined Functions,
5 - Using Functions and Procedures,
A - Example Installation Instructions,


CHAPTER 1

SQL on the iSeries


Structured Query Language (SQL) is the industry standard language for database access. While the American National Standards Institute (ANSI) has a defined SQL standard, within that standard, each relational database platform has its own unique "flavor" of SQL. This can include things as simple as command syntax and as complex as supported functions. In this chapter, we'll explore what makes the iSeries implementation of SQL unique. This chapter acts as an introduction to the SQL functionality on the iSeries. If you're already somewhat familiar with that functionality, you may want to skip ahead to chapter 2.


Software Requirements

To use the examples in this book, you'll need to have the DB2 Query Manager and SQL Development Toolkit licensed program installed on your iSeries. This licensed program gives you a handful of useful tools, including the Query Manager report writing tool as well as the precompilers, which allow you to embed SQL code within other programming languages (SQL RPG, for example).

This licensed program also gives you the STRSQL interactive SQL command line utility. This utility comes in handy if you need to run ad hoc queries (i.e., action queries to modify records in a database on the fly). Throughout this book, many of the examples we cover will be executed from within this utility. Later in the book, we'll also be exploring the use of functions and procedures from client applications developed in Microsoft Access, Visual Basic (VB), and Active Server Pages (ASP).


SQL Tools

Throughout this book, we'll use several utilities (both GUI and green -screen) that make it easier to work with SQL statements on the iSeries. The STRSQL command can be executed from an iSeries command line. This command starts the interactive SQL utility. When you enter this utility, press the F4 key to display a list of the available SQL commands that can be executed from within the interactive SQL session. When a SELECT statement is entered, the resulting data can be either displayed to the screen or sent to a printer or output file. This selection is made by pressing F13 from within STRSQL to display the Interactive SQL Session Services menu and selecting option 1 for Current Session Attributes. Figure 1.1 shows the screen that is displayed.

This screen shows that option 1 has been selected for the "SELECT output" field. If you select option 2, you'll be prompted for the printer name. If you select option 3, you'll be presented with parameters for the file and library to contain the output data. Notice that you can specify other options here as well, such as the data and time format and the naming convention.

iSeries Navigator also includes an SQL tool. You can access this utility by first expanding the Databases option and clicking on your system name and then selecting Run an SQL Script link from the right side of the lower pane in iSeries Navigator. The window shown in Figure 1.2 will be displayed.

You can use this screen in much the same way that you'd use STRSQL. The Examples drop-down list contains templates of SQL statements to perform common tasks. Later, we'll examine using this application to create functions and procedures. As we examine sample SQL statements throughout this book, either of these two tools can be used.


Exploring SQL Statements

Let's take a look at the components and functions of the various SQL statements.


Starting with the Basics

Probably the single most common individual SQL statement is the SELECT statement, which allows you to define a set of data to be retrieved from a database. Figure 1.3 shows the basic structure of a SELECT statement.

Within this example, the FROM clause is required. However, the other clauses shown are optional. The field_list value would be replaced by a comma-delimited list of fields or simply an asterisk to return all fields from the defined table(s). The table_definition value can be a list of joined tables, along with a definition of how they are to be joined, or simply a physical or logical file name. (We'll examine joining tables a little later on.) The condition_expression on the WHERE clause determines exactly which records will be returned from the defined table or tables. The group_field_list used with the GROUP BY clause indicates that the data set returned is to be summarized by the list of fields provided. When a GROUP BY clause is specified, all of the column names appearing in the field_list either must appear in the group_field_list or must be a column function (SUM, AVG, MIN, MAX, etc.). The sort_field_list defined on the ORDER BY clause defines the sort order for the result set. The group_level_conditions defined on the HAVING clause is used in much the same way that the WHERE clause is used except that the condition defined here is checked only after a GROUP BY is processed. This allows you to use a column function as part of a condition.

As you can see, the SELECT statement can take on many forms. The code below shows a very simple form of a SELECT statement.

SELECT
FROM QSYS2.SYSTABLES


This example uses the SQL naming convention in place of the SYS file naming convention, which would use the format library/file. This statement would return all columns (or fields) from the file SYSTABLES in library QSYS2. A slightly more complex version of this statement could be used to select only required fields under specified conditions, as shown below:

SELECT TABLE_NAME, TABLE_TYPE
FROM QSYS2.SYSTABLES
WHERE TABLE_SCHEMA = 'QSYS'
ORDER BY TABLE_NAME


This example would display only the columns TABLE_NAME and TABLE_TYPE for records with a TABLE_SCHEMA value of 'QSYS'. The result set would be sorted by the TABLE_NAME column. To further elaborate on this example, we can add grouping clauses to create a summary result set, as shown below:

SELECT TABLE_SCHEMA, COUNTY(*) AS TABLE_COUNT, SUM(ROW_LENGTH) AS
TOTAL_ROW_LEN
FROM QSYS2.SYSTABLES
WHERE TABLE_TYPE = 'P'
GROUP BY TABLE_SCHEMA
HAVING COUNT(*)>10


This example would display library names (TABLE_SCHEMA) and the number of physical files within each library, along with the total length of rows between all tables within the library. The HAVING clause is used to include only libraries containing more than 10 physical files.

A slight variation of the SELECT statement can be found in SELECT INTO. This statement is used to pass values from a defined SELECT statement into host variables in a stored procedure or embedded SQL within an RPG program.

When using SELECT INTO, no more than one row can be returned at a time. This means that your statement must return results containing unique results.

While the SELECT statement is used to retrieve data from a database, other statements are used to manipulate a database. The DELETE statement, as its name would suggest, is used to remove rows from a table. Figure 1.4 shows the syntax used with the DELETE...

„Über diesen Titel“ kann sich auf eine andere Ausgabe dieses Titels beziehen.