我有一个愚蠢的问题,我无法在Teradata中解决。
我有n个表,对于每个n个表(n> 1000),我必须得到表的列,这些列可能因表而异。
我的问题是:如何从字符串中的一个查询(例如Select columnName from dbc.columns where tablename = table1
)获取结果,让我们说_vColumns
,以便以后能够使用动态SQL语法中的排序值(_vColumns
)?
答案 0 :(得分:3)
我使用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个字符的最大限制...