Skip to content

Instantly share code, notes, and snippets.

@forstie
Created September 18, 2019 23:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save forstie/3442abb3ba50c1aea487b47849189b33 to your computer and use it in GitHub Desktop.
Save forstie/3442abb3ba50c1aea487b47849189b33 to your computer and use it in GitHub Desktop.
For User Defined Table Functions (UDTFs), a trick needs to be employed. Include a where clause whose only purpose is to push the UDTF invocation to the remote database!
--
-- Traditional 3-part name SQL would reference a table, view, or procedure
--
call otherRDB.schema.procedure123();
stop;
insert into localschema.fact_table
select * from otherRDB.remoteschema.fact_table;
stop;
--
-- UDTF query that is run on a remote Db2 for i, and the results returned to the local IBM i
--
select current server, h.*
from table (
qsys2.history_log_info()
) h
where exists
(select 1 from otherRDB.sysibm.sysdummy1);
stop;
@zkarj735
Copy link

zkarj735 commented Nov 8, 2021

I found this gist by accidentally discovering the same behaviour. Do you know if there's a way to force an IBM-supplied TF to run on the local database when a remote database is referenced elsewhere in the statment? I can find no obvious way of qualifying the TF call.

I'm trying to use QSYS2.OBJECT_STATISTICS for the local system, while referencing data from a remote system. LPAR.QSYS2.OBJECT_STATISTICS (or the slashed equivalent) generates a syntax error.

@forstie
Copy link
Author

forstie commented Nov 8, 2021

Hi,
There currently isn't a way to explicitly reference more than one database in a single query.
There are exceptions to the rule for some non-Query statements like INSERT with remote Subselect, and CREATE TABLE AS with remote subselect.
You could also have a UDTF that queries multiple IBM i's and then returns the results, making it appear that a single query can extract, compare, etc multiple IBM i's in a single query.
Regards, Scott

@zkarj735
Copy link

zkarj735 commented Nov 9, 2021

Thanks, I've used the CREATE TABLE AS caveat to effect my requirement. Hooray for QTEMP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment