triston
triston

Reputation: 493

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. I need to generate a report from that.

Upvotes: 2

Views: 4529

Answers (1)

Drew
Drew

Reputation: 2663

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

Upvotes: 1

Related Questions