0

Uncertain if this is even possible, but here goes...

In DB2 v9.7, I'm trying to create a view for every table in a schema.

The view should contain each column, but appends one more timestamp column.

i.e. TABLE1 (COL1, COL2, COL3)

V_TABLE1 (COL1, COL2, COL3, INS_DTTM)

TABLE2 (COL1, COL4)

V_TABLE2 (COL1, COL4, INS_DTTM)

Here's what I've got so far

SELECT 'CREATE VIEW V_' || NAME || ' (' || 
(SELECT LISTAGG(NAME, ', ') FROM SYSIBM.SYSCOLUMNS WHERE TBCREATOR=CREATOR)  || ') 
AS SELECT  ' || (SELECT LISTAGG(NAME, ', ') 
FROM SYSIBM.SYSCOLUMNS WHERE TBCREATOR='SCHEMA') || ', CAST(NULL AS TIMESTAMP) 
AS INS_DTTM FROM ' || CREATOR || '.' || NAME
FROM SYSIBM.SYSTABLES
WHERE CREATOR = 'SCHEMA' AND NAME LIKE 'T%' ORDER BY NAME;

It's my subselect that is causing me grief...

I get the following:

CREATE VIEW V_TABLE1 (COL1, COL2, COL3) AS SELECT COL1, COL2, COL3, CAST(NULL AS TIMESTAMP) AS INS_DTTM FROM SCHEMA.TABLE1
CREATE VIEW V_TABLE2 (COL1, COL2, COL3) AS SELECT COL1, COL2, COL3, CAST(NULL AS TIMESTAMP) AS INS_DTTM FROM SCHEMA.TABLE2

I'll apologize in advance if my syntax is off...I'm retyping across development environs and the internet...so I'm certain I've made mistakes...

I want the columns listed to relate to the table name of that row...but the LISTAGG appears for just the first row...

Hope this makes sense...and if anyone has any suggestions, please let me know.

Thanks

3
  • I'm confused. What is it you're trying to accomplish by creating views of every table? It seems like a great way to make sure the query engine can't optimize your queries very easily. If you're trying to make the DB read-only for certain users, create a role and grant only select permissions to the role and then assign a user to that role.
    – Bacon Bits
    Jan 25, 2015 at 2:18
  • By inserting the 'fake' column in the view, the next mart or DBMS that the data moves to with our ETL tool will update that column based on it's internal clock...some of our destination systems clock are'off'...this way the INS_DTTM is correct for the system it lands on.
    – Pyrrhonist
    Jan 25, 2015 at 14:48
  • If you "want the columns listed to relate to the table name of that row" then you probably should add that condition to the WHERE clause of the subselects, no?
    – mustaccio
    Jan 25, 2015 at 18:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.