2016-03-14 32 views
1

我有一个愚蠢的问题,我无法在Teradata中解决。如何使用分隔符将列结果转换为单个字符串

我有n个表和n个表中的每一个表(n> 1000)我必须得到表的列,这可能因表而异。

我的问题是:我怎样才能得到结果从一个查询在一个字符串(像Select columnName from dbc.columns where tablename = table1),让我们说_vColumns,为了能够在以后的价值在排序(_vColumns)在动态使用SQL语法?

回答

3

我这样做,long ago使用dbc.columns

/*** Rows to concatenated string ***/ 
/*** Nested version instead of hundreds of CASEs. 
     Returns a single concatenated string consisting of up to 2048 
columnnames ***/ 
SELECT 
    databasename 
,tablename 
,max(case when rnk mod 16 = 0 then ColumnName else '' end) || 
    max(case when rnk mod 16 = 1 then ',' || ColumnName else '' end) || 
    max(case when rnk mod 16 = 2 then ',' || ColumnName else '' end) || 
    max(case when rnk mod 16 = 3 then ',' || ColumnName else '' end) || 
    max(case when rnk mod 16 = 4 then ',' || ColumnName else '' end) || 
    max(case when rnk mod 16 = 5 then ',' || ColumnName else '' end) || 
    max(case when rnk mod 16 = 6 then ',' || ColumnName else '' end) || 
    max(case when rnk mod 16 = 7 then ',' || ColumnName else '' end) 
as Columns 
from 
(
    sel 
    databasename 
    ,tablename 
    ,rnk/16 as rnk 
    ,max(case when rnk mod 16 = 0 then ColumnName else '' end) || 
    max(case when rnk mod 16 = 1 then ',' || ColumnName else '' end) || 
    max(case when rnk mod 16 = 2 then ',' || ColumnName else '' end) || 
    max(case when rnk mod 16 = 3 then ',' || ColumnName else '' end) || 
    max(case when rnk mod 16 = 4 then ',' || ColumnName else '' end) || 
    max(case when rnk mod 16 = 5 then ',' || ColumnName else '' end) || 
    max(case when rnk mod 16 = 6 then ',' || ColumnName else '' end) || 
    max(case when rnk mod 16 = 7 then ',' || ColumnName else '' end) || 
    max(case when rnk mod 16 = 8 then ',' || ColumnName else '' end) || 
    max(case when rnk mod 16 = 9 then ',' || ColumnName else '' end) || 
    max(case when rnk mod 16 = 10 then ',' || ColumnName else '' end) || 
    max(case when rnk mod 16 = 11 then ',' || ColumnName else '' end) || 
    max(case when rnk mod 16 = 12 then ',' || ColumnName else '' end) || 
    max(case when rnk mod 16 = 13 then ',' || ColumnName else '' end) || 
    max(case when rnk mod 16 = 14 then ',' || ColumnName else '' end) || 
    max(case when rnk mod 16 = 15 then ',' || ColumnName else '' end) as ColumnName 
    from 
    (
    select 
     databasename 
    ,tablename 
    ,rnk/16 as rnk 
    ,max(case when rnk mod 16 = 0 then ColumnName else '' end) || 
     max(case when rnk mod 16 = 1 then ',' || ColumnName else '' end) || 
     max(case when rnk mod 16 = 2 then ',' || ColumnName else '' end) || 
     max(case when rnk mod 16 = 3 then ',' || ColumnName else '' end) || 
     max(case when rnk mod 16 = 4 then ',' || ColumnName else '' end) || 
     max(case when rnk mod 16 = 5 then ',' || ColumnName else '' end) || 
     max(case when rnk mod 16 = 6 then ',' || ColumnName else '' end) || 
     max(case when rnk mod 16 = 7 then ',' || ColumnName else '' end) || 
     max(case when rnk mod 16 = 8 then ',' || ColumnName else '' end) || 
     max(case when rnk mod 16 = 9 then ',' || ColumnName else '' end) || 
     max(case when rnk mod 16 = 10 then ',' || ColumnName else '' end) || 
     max(case when rnk mod 16 = 11 then ',' || ColumnName else '' end) || 
     max(case when rnk mod 16 = 12 then ',' || ColumnName else '' end) || 
     max(case when rnk mod 16 = 13 then ',' || ColumnName else '' end) || 
     max(case when rnk mod 16 = 14 then ',' || ColumnName else '' end) || 
     max(case when rnk mod 16 = 15 then ',' || ColumnName else '' end) as ColumnName 
    from 
    (
     select 
     databasename 
     ,tablename 
     ,trim(columnName) as ColumnName 
     ,rank() over (partition by databasename, tablename 
        order by columnid) -1 as rnk 
     from 
     dbc.columns 
    ) dt 
    group by 1,2,3 
    )dt 
    group by 1,2,3 
)dt 
group by 1,2 

但是,因为Td14.10你应该使用dbc.Column V代替(如果你超过30个字符有任何列名更长的时间),然后为2048 * 128字符将达到64000个字符的最大限制...

+0

你好dnoeth,我跑你写了什么,但我不知道它是什么;对不起,但我对Teradata不是很熟悉。我的意思是我可以使用“Columns”中的值作为本地字符串变量的值吗?现在我从您的声明中将Columns001.txt作为Columns字段的值,当我点击“Column001.txt”时,我可以看到它的内容,但是作为一个文件......不胜感谢 – BogdanM

+0

@BogdanM:这是SQL Assistant中的一个设置'工具 - 选项 - 数据格式 - 将大小句柄作为CLOB处理,如果大小>',更改为64000并且SQLA将其内联显示... – dnoeth

+0

非常感谢。它像一个魅力 – BogdanM

相关问题