__________
DB2 for i is clearly on a roll with each release and
technology refresh containing new services that help expose and integrate traditional
IBM i functionality through SQL functions, procedures, and views. This article talks about using
LIBRARY_LIST_INFO to solve a programming problem.
A
client that was using system naming asked me if there was SQL syntax to
determine the library in which a file was found on a SELECT statement with an
unqualified reference like “SELECT * FROM filename”. Note that in traditional record level access,
there is something called the Open Feedback area which provides this kind of
information. So there’s good news and
bad news. The bad news is that there
isn’t SQL syntax for determining the library for a file. The good news is that it is easy to build one
using one of the services provided by DB2 for i – the LIBRARY_LIST_INFO view.
If you just specify SELECT * FROM qsys2.library_list_info,
you’ll see it presents a concise version of the library list that you could
also see by doing a Display Library List (DSPLIBL) CL command. It has columns named ORDINAL_POSITION, SCHEMA_NAME,
SYSTEM_SCHEMA_NAME, “TYPE", IASP_NUMBER, and TEXT_DESCRIPTION. For purposes of this query, we only need to
use ORDINAL_POSITION and SCHEMA_NAME. We could alternatively use SYSTEM_SCHEMA_NAME if we wanted to be sure
of getting the ten character name versus the potentially 128 character SQL
schema name.
Let’s say our RPG program is opening a table named XYZ under system naming and wanted to know programmatically which instance of XYZ is being opened. To make this work, we just have to join the catalog SYSTABLES which contains all the instances of the XYZ file to the LIBRARY_LIST_INFO to see which one(s) are in our library list and which one would be found first. The query isn’t complicated. This would show you all the instances of XYZ in the library list based on the order they appear in the library list:
SELECT table_name, table_schema,
ordinal_position
FROM qsys2.library_list_info INNER JOIN qsys2.systables
ON schema_name = table_schema
WHERE table_name = 'XYZ'
ORDER BY ordinal_position ;
FROM qsys2.library_list_info INNER JOIN qsys2.systables
ON schema_name = table_schema
WHERE table_name = 'XYZ'
ORDER BY ordinal_position ;
We love modular programming and this seems like something we
might want to write once and then use in many places. Let’s put a function wrapper around it. Here’s a scalar function called SchemaOfTable
which will return the single value indicating the schema of the first instance
of XYZ in the library list. If it
doesn’t find one, it returns the null value.
CREATE OR REPLACE FUNCTION
SchemaOfTable (i_tablename VARCHAR(128))
RETURNS VARCHAR(128)
SPECIFIC SchemOfTbl
LANGUAGE SQL
RETURN
(SELECT table_schema
FROM qsys2.library_list_info INNER JOIN qsys2.systables
ON schema_name = table_schema
WHERE table_name = i_tablename
ORDER BY ordinal_position FETCH FIRST 1 ROW ONLY) ;
RETURNS VARCHAR(128)
SPECIFIC SchemOfTbl
LANGUAGE SQL
RETURN
(SELECT table_schema
FROM qsys2.library_list_info INNER JOIN qsys2.systables
ON schema_name = table_schema
WHERE table_name = i_tablename
ORDER BY ordinal_position FETCH FIRST 1 ROW ONLY) ;
We can invoke this function as part of another SELECT. Here’s the most basic example to just
retrieve the value.
SELECT SchemaOfTable (‘XYZ’) FROM sysibm.sysdummy1;
You’ve probably heard this many times from the DB2 for i team – we are continuously enhancing our support and there’s a lot of function provided that could make your life simpler. The system-supplied services provide an open-ended opportunity to expose and integrate IBM i functions using SQL techniques and best practices. And it’s fun!
Well I tried the first select (SELECT * FROM qsys2.library_list_info) and I get a message that the table can't be found. Tried it in STRSQL and run SQL Scripts. Then I looked at tables in QSYS2 using strpdm. Nothing like that appears to be there. I'm on V5R4 - yes, yes, I know. Is all this stuff newer?
ReplyDeleteDave, 5.4 (and 6.1 for that matter) are out of service and no longer supported. Try a current version of DB2 for i: 7.1 and 7.2.
ReplyDeleteIf you need assistance with moving forward to a release that is less than a decade old, please let us know. Thanks.
Good post Mike, very useful and with your scalar function is very simple !
ReplyDelete---
Roberto