Teradata 如何使用分隔符将类似列的结果转换为单个字符串

Teradata 如何使用分隔符将类似列的结果转换为单个字符串,teradata,Teradata,我有一个愚蠢的问题,我无法用Teradata解决 我有n个表,对于n个表中的每一个(n>1000),我必须得到表的列,每个表的列可能不同 我的问题是:如何从一个字符串(比如说\u vColumns)中的一个查询(比如从dbc.columns中选择columnName,其中tablename=table1)中获得结果,以便以后能够在动态SQL语法中使用排序(\u vColumns)中的值 我使用dbc.columns /*** Rows to concatenated string ***/

我有一个愚蠢的问题,我无法用Teradata解决

我有n个表,对于n个表中的每一个(n>1000),我必须得到表的列,每个表的列可能不同

我的问题是:如何从一个字符串(比如说
\u vColumns
)中的一个查询(比如
从dbc.columns中选择columnName,其中tablename=table1
)中获得结果,以便以后能够在动态SQL语法中使用排序(
\u vColumns
)中的值

我使用
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.ColumnV(如果您有任何超过30个字符的列名),然后2048*128个字符将达到64000个字符的最大限制…

您好,dnoeth,我运行了您编写的内容,但我不确定它是什么;对不起,我对Teradata不是很熟悉。我的意思是,我可以使用“Columns”中的值作为本地字符串变量的值吗?现在,我将文本“Columns001.txt”作为您语句中Columns字段的值,当我单击“Column001.txt”时,我可以看到它的内容,但不知何故,它是一个文件…很多thanks@BogdanM:这是SQL助手
工具-选项-数据格式-将Varchar列作为CLOB处理(如果大小>
)中的设置,更改为64000,SQLA将以内联方式显示它。。。