t-sql表列到String

时间:2013-04-24 16:52:18

标签: sql sql-server tsql

是否可以从information_schema中递归地动态地对表列标题进行字符串

因此,如果我的数据库中有2个表(或多个表),每个表有5列

我是否可以通过架构获取查询以查找此表,然后将所有表列的听众串到另一个表中,其结尾为

table name ¦ string 
Table A    ¦ id,columnName1,columnName2 ,columnName3 ,columnName4 ,columnName5
Table b    ¦ id,columnName1,columnName2 ,columnName3 ,columnName4 ,columnName5

(这两行是BTW)

5 个答案:

答案 0 :(得分:2)

FOR XML PATH('')sys.columnssys.tables结合使用的内容可行:

SELECT t.name [TableName], 
(
    SELECT name + ','
    FROM sys.columns c
    WHERE c.object_id = t.object_id
    FOR XML PATH('')
) [String]
FROM sys.tables t

它基本上从查询返回的列和格式为XML,其中元素标记在PATH('')中定义。但是,如果将其留空,则会省略标记并返回字符串。

documentation on MSDN还有许多不同的示例,其中包含FOR XML子句的其他用法。

答案 1 :(得分:1)

使用 sys.objects 更有效 sys.tables 。这对你有帮助 -

SELECT 
      table_name = s.name + '.' + o.name
    , [columns] = STUFF((
        SELECT ', ' + c.name
        FROM sys.columns c WITH (NOWAIT)
        WHERE c.[object_id] = o.[object_id]
        FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '')
FROM (
    SELECT 
          o.[object_id]
        , o.name
        , o.[schema_id]
    FROM sys.objects o WITH (NOWAIT)
    WHERE o.[type] = 'U'
        AND o.is_ms_shipped = 0
) o
INNER JOIN sys.schemas s WITH (NOWAIT) ON o.[schema_id] = s.[schema_id] 
ORDER BY 
      s.name
    , o.name

答案 2 :(得分:1)

来吧伙计们,它可以更简单,没有XML:

declare @columns varchar(1000)
set @columns=''

select @columns = @columns + ',' + name
from sys.columns 
where object_id = object_id('MyTable')

答案 3 :(得分:0)

这样做(虽然可能有另一种方式):

;with cte as
(
    Select  TableName = t.TABLE_NAME
            ,ColumnList = Substring((Select ', ' + c2.COLUMN_NAME
                                    From    INFORMATION_SCHEMA.COLUMNS c2
                                    Where   c.TABLE_NAME = c2.TABLE_NAME
                                    For XML Path('')
                                    ), 3, 8000)
            ,ROW_NUMBER() OVER(Partition By t.TABLE_NAME Order By t.TABLE_NAME) RowNumber
    From    INFORMATION_SCHEMA.TABLES t
    join    INFORMATION_SCHEMA.COLUMNS c
        on  t.TABLE_NAME = c.TABLE_NAME
)
Select  TableName
        ,ColumnList
From    cte
Where   RowNumber = 1

答案 4 :(得分:0)

我认为您希望使用此Query来制作数据库中所有表格的类文件。 因此回答

if object_id('tempdb..#t') is not null
    drop table #t

SELECT 
T.name Table_name
,ltrim(rtrim(C.name)) Column_name
,TYPE1.name AS [type_name]
,C.max_length length1
into #t
FROM sys.tables T WITH(NOLOCK)
INNER JOIN sys.columns C WITH(NOLOCK) ON T.OBJECT_ID=C.OBJECT_ID
INNER JOIN sys.types AS TYPE1 with(nolock) ON c.user_type_id=TYPE1.user_type_id
ORDER BY T.name

SELECT 
  #t.Table_name,
  STUFF((
    SELECT ', ' + t2.Column_name+' as ' +t2.type_name + '('+ cast(t2.length1 as varchar) + ')'
    FROM #t t2
    WHERE t2.Table_name = #t.Table_name
    FOR XML PATH (''))
,1,2,'') AS Names
FROM #t
GROUP BY #t.Table_name;

Question

的具体答案
if object_id('tempdb..#t') is not null
    drop table #t

SELECT 
T.name Table_name
,ltrim(rtrim(C.name)) Column_name
into #t
FROM sys.tables T WITH(NOLOCK)
INNER JOIN sys.columns C WITH(NOLOCK) ON T.OBJECT_ID=C.OBJECT_ID
where t.name in ('[Table A]','[Table B]')
ORDER BY T.name

SELECT 
  #t.Table_name,
  STUFF((
    SELECT ', ' + t2.Column_name
    FROM #t t2
    WHERE t2.Table_name = #t.Table_name
    FOR XML PATH (''))
,1,2,'') AS Names
FROM #t
GROUP BY #t.Table_name;