Search code examples
schemaodbcdb2aliasmainframe

How can I create a schema alias in DB2 on System z?


Part of a reporting toolkit we use for our development is configured to always use the same schema (say XYZZY).

However, certain customers have stored their data in a different schema PLUGH. Is there any way within DB2/z to alias the entire schema XYZZY to refer to the objects in schema PLUGH?

The reporting toolkit runs on top of ODBC using the DB2 Connect Enterprise Edition or Personal Edition 9.1 drivers.

I know I can set up individual aliases for tables and views but we have many hundreds of these database objects and it will be a serious pain to do the lot. It would be far easier to simply have DB2 auto-magically translate the whole schema.

Keep in mind we're not looking for being able to run with multiple schemas, we just want a way to redirect all requests for database objects to a single, differently named, schema.

Of course, if there's a way to get multiple schemas on a per-connection basis, that would be good as well. But I'm not helpful.


Solution

  • I am guessing that by DB/2 schema you mean the qualifying name in some two part object name. For example, if a two part table name is: PLUGH.SOME_TABLE_NAME. You want to do define XYZZY as an alias name for PLUGH so the reporting program can refer to the table as XYZZY.SOME_TABLE_NAME.

    I don't know how to directly do that (schema names don't take on aliases as far as I am aware). The objection you have to defining individual alias names using something like:

    CREATE ALIAS XYZZY.SOME_TABLE_NAME FOR PLUGH.SOME_TABLE_NAME 
    

    is that there are hundreds of them to do making it a real pain. Have you thought about using a SELECT against the DB/2 catalogue to generate CREATE ALIAS statements for each of the objects you need to refer to? Something like:

    SELECT 'CREATE ALIAS XYZZY.' || NAME || ' FOR PLUGH.' || NAME
    FROM SYSIBM.SYSTABLES
    WHERE CREATOR = 'PLUGH'
    

    Capture the output into a file then execute it. Might be hundreds of commands, but at least you didn't have to write them.