获取列名和按列名分组的类型

时间:2018-10-04 12:21:08

标签: sql db2

我有IBM DB2数据库。我想获取所有列名,类型长度和按表名分组的小数位数。

要从架构XYZ获取所有表:

select name
from SYSIBM.SYSTABLES
where creator = 'XYZ';

现在我可以得到给定表的列描述:

SELECT NAME, COLTYPE, LENGTH, SCALE
FROM SYSIBM.SYSCOLUMNS
WHERE TBNAME = 'sometablename'

我想将其分组:

SELECT NAME, COLTYPE, LENGTH, SCALE
FROM SYSIBM.SYSCOLUMNS
WHERE TBNAME in (select name from SYSIBM.systables where creator = 'XYZ') 
GROUP BY table_names_from_schema_xyz;

如何做到?

3 个答案:

答案 0 :(得分:1)

分组(从SQL的意义上来说)仅在聚合函数的上下文中才有意义。 我怀疑您要查找的是按表名排序的输出,然后是列名排序,因此同一表的所有列都被“分组”在一起。

此查询可能对您有用。

SELECT T.NAME AS TABNAME, C.NAME AS COLNAME, COLTYPE, LENGTH, SCALE
FROM SYSIBM.SYSTABLES T, SYSIBM.SYSCOLUMNS C
WHERE T.NAME = C.TBNAME
AND CREATOR = 'XYZ'
ORDER BY T.NAME, C.NAME;

答案 1 :(得分:1)

您的问题只能通过SYSCAT.COLUMNS回答

select tabname, colname, typename, length, scale   
 from syscat.columns
where tabschema = 'XYZ' 
order by tabname, colno

答案 2 :(得分:0)

尝试使用 SYSIBM.systables 进行内部联接,也许下面的示例应该起作用

   select c.colname,
            t.tabname as tables, COLTYPE, LENGTH, SCALE
    from SYSIBM.SYSCOLUMNS c
    inner join SYSIBM.systables t on 
          t.tabschema = c.tabschema and t.tabname = c.tabname
    where t.type = 'T'
    and t.tabschema = 'XYZ'

    order by c.colname;