
Structured Query Language (SQL) is playing an ever-increasing IBM i role in enterprise database design, supplanting legacy DDS. This course shows you how to
- use industry-standard SQL Data Definition Language to build an IBM i database
- retrieve, analyze, manipulate, and modify your data using SQL Data Manipulation Language
- embed SQL statements into your ILE RPG programs to add the power of SQL to your RPG applications.

Prerequisites: This course is appropriate for experienced ILE RPG (RPG IV) programmers interested in learning how to embed SQL statements in an ILE RPG program.
The course assumes you are already familiar with free format ILE RPG. If you are not familiar with current ILE RPG syntax, you may find the ILE RPG in Easy Bytes series more appropriate. That series also includes information, albeit somewhat less detailed, about embedded SQL.
How to Take the Course
The course is made up of a number of short lessons covering various aspects of the main course topic. If you are enrolled in the course, you will be able to view all its lessons. To view a lesson, select it from the menu below.
We recommend that you take the following approach to each lesson:
- Preview. Many of the lessons have one or more audio “previews” that you can use to familiarize yourself with the content of the lesson (or a section of the lesson). Most of these audio previews are less than five minutes long.
- Read. After you’ve listened to the audio for a section, study that section to get a more detailed understanding of the topic and and study some examples. When you are finished with a lesson, be sure to “Mark Complete” to let us know you’ve successfully completed it. As you complete each lesson, a progress bar will measure your passage through the course. Even after you’ve marked a course complete, you can still review it later.
- Review. Some of the lessons will have quiz questions to test your knowledge of the topic. You must pass the quiz with a score of 80% or better to move on. If you don’t understand why you answered incorrectly, you can “View Details” for an explanation, and then go back and review the course.
Tips
At any time, even if you are not viewing a lesson, you may use the Search function at the top of the page to search the lessons for a specific term. If you are enrolled in the lesson which contains the term, you may go directly to that lesson by selecting the appropriate search result. If you are not enrolled, when you select the search result, you will be invited to enroll. (Remember, though, you must take the units in order.)
At the end of each lesson is an outline of the course, with the completed lessons checked. You may revisit any completed lesson by selecting it, or you may go on to the next (unchecked) lesson.
At the end of each lesson, if you have any questions regarding that lesson, use the “Questions?” form to submit a question. We’ll try to answer your questions in a timely manner.
One-Time Payment
Onetime payment … lifetime access!
Introduction to SQL
IBM i Database Overview. A database is a collection of related information, organized to facilitate processing of data to serve a specific function. The IBM i operating system is unique in the way it handles data. Unlike other systems, which require additional, costly software to provide them with database capabilities, IBM i was designed with database applications in mind, and the database is tightly integrated into the operating system.
IBM i programmers use a variety of utilities to generate and process SQL statements, but the three primary options are as follows: Interactive SQL, RUNSQLSTM (Run SQL Statements) command, IBM Data Studio.
Data Definition Language (DDL)
You use Data Definition Language (DDL) statements to create new objects, such as tables, and to alter the structure or properties of existing database objects. DDL statements concern themselves with creating and defining the database objects themselves. SQL tables contain the actual data in a database. When SQL creates a new table, the database creates a single-member physical file object in a library. Tables are arranged into rows (records) and columns (fields).
You must assign a data type to each column in a table. The column’s data type determines how its values are stored, how much storage the column occupies, and what kinds of operations can be performed on the column when it is used within a program. Commonly used data types fall into three general categories: Character, Numeric, Date.
SQL views provide alternative ways of accessing the data in a table. Although a program can retrieve and process virtual rows from a view, the view does not actually contain data. Instead, it stores an access path, a sequence of pointers to the actual data in one or more tables. When SQL creates a new view, the database constructs an unkeyed logical file based on a physical file.
Data Manipulation Language (DML)
SQL categorizes a number of statements as Data Manipulation Language (DML). While DDL statements deal with the database objects themselves, DML statements retrieve and manage the data within the tables (physical files).
A SELECT query lets you view the data in a table, but how did those records get in the database in the first place? Use the INSERT query to create new records.
ILE RPG With Embedded SQL
In recent years, it has become common practice to embed SQL statements within an RPG program to access and manage its database processing. Generally, these statements are DML statements, but they can include DDL as well as a few SQL statements specifically designed for use in a program.
When RPG executes an SQL statement, SQL returns to the program several feedback tools that you can use to diagnose the results of executing the statement. If the SQL statement ends in error, the program does not stop but continues to run with the next subsequent statement. So, it’s important to check the diagnostic response before continuing with the RPG program. The SQL Communication Area (SQLCA) is a data structure that contains return codes, subfields with valuable diagnostic information.
Embedded SQL is an excellent choice for set-at-a-time processing (i.e., treating entire groups of rows in one statement). Other SQL statements—notably, Select Into—work well for processing a single row from a table or view. But at times, a program may need to retrieve multiple rows into a result set and then process each of those rows individually. For those cases, sometimes known as row-at-a-time processing, SQL provides a mechanism called a cursor.
This ILE RPG example explains and shows the steps to use an SQL cursor in an SQLRPGLE program. Copy and use as a template for your own SQLRPGLE program.
The SQL Fetch statement retrieves a row from a multiple-row result set into host variables or a host structure (or both). This type of Fetch is called a single fetch. If the host structure is an array data structure, Fetch can perform a multiple-row fetch, retrieving several rows with one statement and placing the result set into individual elements of the array data structure.
Up to now, all the embedded SQL statements we’ve discussed have been static SQL. With static SQL, the basic structure of each SQL statement is known at the time of program compilation. The SQL statement can use host variables to substitute values at runtime, but its general purpose and construction don’t change once the program is created. Alternatively, an RPG program can build a complete SQL statement as a character string by using data in the program. The resulting dynamic SQL statement is prepared at runtime, rather than when the program is created
In addition to the Sqlcode, Sqlstate, Sqlwarn, and Sqlerrd diagnostic feedback, SQL also supports the Whenever statement to specify an action to take when an exception occurs. A Whenever statement serves as a form of monitor for an SQLRPGLE program, watching for an exception in the subsequent SQL statements and indicating the next statement to process when the exception arises.
The SQL Set Option statement establishes processing options to use during a program that uses embedded SQL.
Legacy Code (SQL)
Earlier versions of ILE RPG (prior to IBM i Release 5.4) used an alternative compiler directive for embedding SQL statements. Other than the alternative fixed format syntax, all the same concepts for embedded SQL apply to those programs.