In bind processing, the target PLAN_TABLE, which is the current sqlid.PLAN_TABLE, and the target tables that will be processed by the SQL statements are qualified separately. However, SQL EXPLAIN processing requires that you combine these processes when tables are qualified. If the qualifying process is not done correctly, the tables will be created with the wrong qualifier.
The PLAN_TABLE is qualified by the user ID in the owner parameter, and the application tables are qualified by the user ID that is specified for the qualifier parameter.
-- THIS WILL EXAMINE ALL THE TABLES FOR CREATOR ID = PUBLIC01
-- AND CREATE A SYNONYM FOR THAT TABLE UNDER P390H IF
-- THERE ISN'T A SYNONUM OR ALIAS FOR THAT TABLE NAME ALREADY
-- NOTICE THAT THE SUBSELECT FROM SYSTABLES DOESN'T QUALIFY
-- THE TYPE VALUE BECAUSE EITHER AN ALIAS, TABLE OR VIEW
-- WILL PREVENT THE CREATION OF A NEW SYNONYM
SELECT 'CREATE SYNONYM ' CONCAT RTRIM(NAME) CONCAT ' FOR '
CONCAT RTRIM(CREATOR) CONCAT '.' CONCAT RTRIM(NAME)
CONCAT ';'
FROM SYSIBM.SYSTABLES A
WHERE CREATOR = 'PUBLIC01'
AND NOT EXISTS (SELECT 1 FROM SYSIBM.SYSSYNONYMS B
WHERE B.CREATOR = 'P390H'
AND B.NAME = A.NAME)
AND NOT EXISTS (SELECT 1 FROM SYSIBM.SYSTABLES C
WHERE C.CREATOR = 'P390H'
AND C.NAME = A.NAME)
ORDER BY 1