how can I print details (table name, column name, data type) of each column/table in my db2 database?

我怕爱的太早我们不能终老 提交于 2019-12-11 14:09:07

问题


In my previous question Mark suggested a good answer for displaying count on every table in my database. I would like to expand this procedure and - instead of counts - display the specific info (TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE) about each column in the database.

I so far have the following command:

--#SET TERMINATOR @
CREATE OR REPLACE FUNCTION EXPORT_SCHEMAS()
RETURNS TABLE (P_TABSCHEMA VARCHAR(128), P_TABNAME VARCHAR(128), P_COLUM_NNAME VARCHAR(128), P_DATA_TYPE VARCHAR(128))
BEGIN
  DECLARE L_STMT VARCHAR(256);
  DECLARE L_ROWS VARCHAR(256);

  FOR V1 AS
    SELECT TABSCHEMA, TABNAME
    FROM SYSCAT.TABLES
    WHERE TYPE = 'T'
    ORDER BY 1,2
  DO
    SET L_STMT = 'SET ? = (SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE FROM SYSIBM.COLUMNS where TABLE_NAME = "'||V1.TABNAME||'" AND TABLE_SCHEMA = "'||V1.TABSCHEMA||'")';
    PREPARE S FROM L_STMT;
    EXECUTE S INTO L_ROWS;
    PIPE(L_ROWS);
  END FOR;
  RETURN;
END@

SELECT * FROM TABLE(EXPORT_SCHEMAS())@

but now when I run it:

db2 -ntd~ -f export_schemas.sql > dump.csv

I'm getting the error:

DB21034E  The command was processed as an SQL statement because it was not a
valid Command Line Processor command.  During SQL processing it returned:
SQL20019N  The result type returned from the function body cannot be assigned
to the data type defined in the RETURNS clause.  LINE NUMBER=17.
SQLSTATE=42866

Could you please help me and let me know what is wrong here and how could I fix it? Thanks!


回答1:


If you use Db2 for LUW, then you shouldn't use SYSIBM schema in your queries on the system catalog. Use SYSCAT instead.

You don't have to use any functions to get what you want here. Use the following query instead:

SELECT TABSCHEMA, TABNAME, COLNAME, TYPENAME
FROM SYSCAT.COLUMNS
ORDER BY TABSCHEMA, TABNAME, COLNO;

As for your routine. There is a number of errors in the text.

1) if you want to assign multiple values with SET statement, you must use the corresponding number of parameter markers in the statement:

SET (?, ..., ?) = (SELECT COL1, ..., COLn FROM ...);
PREPARE S FROM L_STMT;
EXECUTE S INTO L_V1, ..., L_Vn;

2) RETURNS TABLE (...) and PIPE(...) must have the same number of columns




回答2:


You could directly query the tables SYSCAT.COLUMNS and SYSCAT.TABLES. The following returns the table schema and name followed by column name and their type. Column info is sorted by the column order:

select t.tabschema, t.tabname, c.colname, c.typename, c.colno
from syscat.columns c, syscat.tables t
where t.type='T' and t.tabname=c.tabname and t.tabschema=c.tabschema
order by 1,2,c.colno

BTW: Db2 has a tool db2look to export schema information.



来源:https://stackoverflow.com/questions/58813236/how-can-i-print-details-table-name-column-name-data-type-of-each-column-tabl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!