Guru: Three Ways To Manage Unmatched Data
April 24, 2017 Ted Holt
Heaven forbid that I would ignore a failed RPG CHAIN (random read) operation. I always take appropriate action. Which action I take depends on the situation. The same applies to outer joins that don’t find matching data in a secondary table. Here are three ways to deal with unmatched data in an outer join using SQL.
To illustrate, let’s use three tables from an overly simplified general ledger system. The first is a table of departments into which the business is divided. The second is a chart of accounts. The third is a transaction file that feeds the general ledger. Those tables look like this:
select ID, Name from dept
select ID, Name from acct
|1000||Cash in bank|
select * from glxacts
Notice that the department ID and account number in transaction 3 do not match any rows in the department and chart of accounts tables.
Method 1: Deal With Null Values
By default, the database manager returns nulls for the columns of a secondary table when no row matches a row in the primary table. Here’s such a query:
select t.ID, t.date, t.dept, d.name as Department, t.account, a.name as Account, t.amount from glxacts as t left join dept as d on t.dept = d.ID left join acct as a on t.account = a.ID
The department name and account name are null for row 3 in the result set. There is nothing wrong with this. You must consider that the DEPARTMENT and ACCOUNT columns may have null values, and program accordingly. RPG programs require the use of null indicator variables, which are five-digit integers. A negative value means that the corresponding column is null.
Method 2: Fill In Default Values
Dealing with nulls can be a nuisance. You can use the COALESCE, VALUE, and IFNULL functions to convert nulls to non-null values. These three functions return the first non-null value in the list of arguments (parameters). If all of the arguments are null, these functions return the null value. I prefer COALESCE because it is the most standard of the three and because it can accept more than two arguments.
select t.ID, t.date, t.dept, coalesce(d.name, '**INVALID**') as Department, t.account, coalesce(a.name, '**INVALID**') as Account, t.amount from glxacts as t left join dept as d on t.dept = d.ID left join acct as a on t.account = a.ID
Method 3: Raise An Error
You may tell a query to cancel itself when it finds unmatched data. After all, why return multitudes of rows if the result set will be unusable?
select t.ID, t.date, t.dept, coalesce(d.name, raise_error('88001', 'Bad department ' concat varchar(t.dept) concat ' for ID ' concat varchar(t.ID ))) as Department, t.account, coalesce(a.name, raise_error('88002', 'Bad account ' concat varchar(t.account) concat ' for ID ' concat varchar(t.ID ))) as Account, t.amount from glxacts as t left join dept as d on t.dept = d.ID left join acct as a on t.account = a.ID
The RAISE_ERROR function forces an SQL statement to end abnormally. It has two arguments: the SQL state to be returned and the message text to describe the error. I have placed this function as the second argument in the COALESCE functions. If the department column of the transaction table matches the department ID in at least one row of the departments table, the result set contains the department name. Otherwise, the database manager cancels the query with a SQL state value of 88001. An unmatched account number cancels with SQL state 88002. The SQL code value in those cases is -438. In the job log you’ll see messages CPF503E:
User-defined function error on member GLXACTS. The external program or service program returned SQLSTATE 88001. The text message returned from the program is: Bad department 5 for ID 3.
Message Bad department 5 for ID 3 returned from SIGNAL, RESIGNAL, or RAISE_ERROR.
These are all good ways to deal with unmatched data. It’s good to have options.