简体   繁体   English

从具有列名和描述的表中选择数据SQL Server

[英]Select data from table with column name and description SQL Server

I have a legacy table with all column named in an old way, the names don't make sense to others, but the table description contains column description, how to can select all data from the table, and combine with the column description? 我有一个旧表,所有列都以旧方式命名,名称对其他人没有意义,但是表描述包含列描述,如何从表中选择所有数据,并与列描述结合?

UPDATED BELOW: 更新如下:

To get the Names and Columns Description 获取名称和列描述

SELECT 
 COLUMN_NAME AS Name, 
 COLUMN_TEXT AS Description
FROM 
 [DB2-LINKED-SERVER].[BD2].QSYS2.SYSCOLUMNS 
WHERE  
 TABLE_NAME = 'ITMHED'

I got: 我有:

Name      Description
ITMNO     Item Number
ITMNM     Item Name
 .... 800+ rows more

Then I have another query: 然后我有另一个查询:

SELECT * FROM [DB2-LINKED-SERVER].[BD2].ITMHED

It returned me: 它返回了我:

ITMNO      ITMNM           ...800+ more columns
AB-001     Mountain Bike             ....

What I want to get: 我想得到什么:

Item Number      Item Name      ...800+ more columns
AB-001           Mountain Bike     .....

If I need only 2-3 column, I can manually rename them, but with that many record, I want to make it more readable for users. 如果我只需要2-3列,则可以手动重命名它们,但是有那么多记录,我想使它对用户更具可读性。 I need to generate a report from that. 我需要据此生成报告。

SELECT 
 COLUMN_NAME AS Name + ' as '+ 
 COLUMN_TEXT AS Description + ','
FROM 
 [DB2-LINKED-SERVER].[BD2].QSYS2.SYSCOLUMNS 
WHERE  
 TABLE_NAME = 'ITMHED'

Could get the output from that and then insert it into the following: 可以从中获取输出,然后将其插入以下内容:

select (insert the output from above here) from [DB2-LINKED-SERVER].[BD2].ITMHED 从[DB2-LINKED-SERVER]中选择(从上面插入输出)。[BD2] .ITMHED

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM