0

如何检索正在执行选择的表的列名?我正在尝试将结果附加到每个字段名称的先前结果中。

示例(不起作用):

declare @result as nvarchar(max)
concat(select distinct parse(@result, COLName(field1), N':' from sometable, field1))

一些表:

id,unit
`---------`
1,quart
2,gallon
3,liter

结果

id:1、2、3
单位:夸脱、加仑、升

我实际上是在尝试使用以下 select 语句对表中包含文本、varchar、char 且大小小于 4000 的每个字段执行此操作:

我正在尝试解析的表的实际字段列表(这有效):

select top 1000 SCHEMA_NAME([o].schema_id) as [Schema Name],
       [s].name as [Table Name],
       [c].name as [Field Name],
       [t].name as [Type],
       [c].max_length as [Length],
       [c].precision,
       [c].scale,
       [c].is_identity
from sys.sysobjects as [s]
inner join sys.all_columns as [c] on [s].id = [c].object_id
inner join sys.systypes as [t] on [c].system_type_id = [t].xtype 
inner join sys.objects as [o] on [s].id = [o].object_id
where [s].xtype like 'U' and [t].name not like 'sysname%' and [s].name=N'TableName'

这样我就可以清楚地看到具有不同结果集的每个字段的名称。

4

1 回答 1

1

如果您事先知道字段列表,则可以使用以下内容:

SELECT
(
    SELECT cast(id as varchar(50)) + ','
    FROM sometable
    ORDER by id
    FOR XML PATH('')
) as list1

UNION ALL

SELECT
(
    SELECT unit + ','
    FROM sometable
    ORDER by id
    FOR XML PATH('')
) as list2

...
于 2013-06-21T11:24:09.013 回答