2016-03-14 74 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

相關問題