简体   繁体   English

如何打印 db2 数据库中每个列/表的详细信息(表名、列名、数据类型)?

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

In my previous question Mark suggested a good answer for displaying count on every table in my database. 在我之前的问题中,Mark 提出了一个很好的答案,用于在我的数据库中的每个表上显示计数。 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.我想扩展这个过程并且——而不是计数——显示关于数据库中每一列的特定信息(TABLE_SCHEMA、TABLE_NAME、COLUMN_NAME、DATA_TYPE)。

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!谢谢!

If you use Db2 for LUW, then you shouldn't use SYSIBM schema in your queries on the system catalog.如果将 Db2 用于 LUW,则不应在系统目录的查询中使用SYSIBM模式。 Use SYSCAT instead.请改用SYSCAT

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: 1)如果要使用SET语句赋值多个值,必须在语句中使用相应数量的参数标记:

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) RETURNS TABLE (...)PIPE(...)的列数必须相同

You could directly query the tables SYSCAT.COLUMNS and SYSCAT.TABLES .您可以直接查询SYSCAT.COLUMNSSYSCAT.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.顺便说一句:Db2 有一个工具db2look来导出模式信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从DB2中的列获取表名? - How to get table name from column in DB2? 如何获取.NET表中各列的列名和数据类型列表? - How would I get a list of column name & data type for each column in a table in .NET? 我可以在“喜欢”或“包含”运算符/函数中使用表列名称作为db2中的搜索参数吗? - Can I use a table column name as a search argument in db2 “like” or “contain” operator/function 有什么方法可以将表数据用作DB2数据库中的单独列 - Is there any way to use the table data as separate column in DB2 database 如何更改DB2中的列名 - How to change the column name in DB2 如何使用特定表中的列作为表名,我需要从中删除或插入或更新我的数据 - How to use a Column in a particular table as Table name from which i need to delete or insert or update my data DB2动态表名称 - DB2 dynamic table name 如何使用列名查找数据库名和表名 - How to find the database name and table name by using Column name 我怎样才能在一个表中有一个名为 table_name 的列,我可以用它来引用 mysql DB 中的其他表? - How can I have a column called table_name in one table which I can use to refer to other tables in mysql DB? 如何遍历具有特定名称的所有表,然后更新每个表中的特定列值 - How can i traverse all the tables with specific name and then update a particular column value from each table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM