Question

I want to query in SQL Server a column's name. I know it is possible to get a table's columns from the system table, but unfortunately that's not enough for me.

Example:

I have a table that contains an ID column and a string column. The table's name is test, and it has a testID and a test column.

This query:

select column_name 
from information_schema.columns 
where table_name = 'teszt'

return the names of the columns of my table. So it returns testID and Test.

What I want is when I use a query like this:

select count(*) as Amount from test 

I want a query that can return the column names of my query. So in this specific case it returns the string 'Amount'. I don't know if that is possible.

Was it helpful?

Solution

Not sure if there is an easier way of getting the name of columns with aliases, but one way of doing it is via XML. This query will return one row per column in the inner query:

select T1.res.value('local-name(.)', 'varchar(50)')
from (select cast(
(
    select count(*) as Amount from test
    for xml raw) as xml
)) q(res)
CROSS APPLY q.res.nodes('/row/@*') as T1(res)

OTHER TIPS

In SQL Server 2012 you have a stored procedure that you can use for exactly this purpose.

sp_describe_first_result_set (Transact-SQL)

SQL Fiddle

MS SQL Server 2012 Schema Setup:

create table test(id int);

Query 1:

exec sp_describe_first_result_set N'select count(*) as Amount from test'

Results:

| IS_HIDDEN | COLUMN_ORDINAL |   NAME | IS_NULLABLE | SYSTEM_TYPE_ID | SYSTEM_TYPE_NAME | MAX_LENGTH | PRECISION | SCALE | COLLATION_NAME | USER_TYPE_ID | USER_TYPE_DATABASE | USER_TYPE_SCHEMA | USER_TYPE_NAME | ASSEMBLY_QUALIFIED_TYPE_NAME | XML_COLLECTION_ID | XML_COLLECTION_DATABASE | XML_COLLECTION_SCHEMA | XML_COLLECTION_NAME | IS_XML_DOCUMENT | IS_CASE_SENSITIVE | IS_FIXED_LENGTH_CLR_TYPE | SOURCE_SERVER | SOURCE_DATABASE | SOURCE_SCHEMA | SOURCE_TABLE | SOURCE_COLUMN | IS_IDENTITY_COLUMN | IS_PART_OF_UNIQUE_KEY | IS_UPDATEABLE | IS_COMPUTED_COLUMN | IS_SPARSE_COLUMN_SET | ORDINAL_IN_ORDER_BY_LIST | ORDER_BY_IS_DESCENDING | ORDER_BY_LIST_LENGTH | TDS_TYPE_ID | TDS_LENGTH | TDS_COLLATION_ID | TDS_COLLATION_SORT_ID |
|-----------|----------------|--------|-------------|----------------|------------------|------------|-----------|-------|----------------|--------------|--------------------|------------------|----------------|------------------------------|-------------------|-------------------------|-----------------------|---------------------|-----------------|-------------------|--------------------------|---------------|-----------------|---------------|--------------|---------------|--------------------|-----------------------|---------------|--------------------|----------------------|--------------------------|------------------------|----------------------|-------------|------------|------------------|-----------------------|
|         0 |              1 | Amount |           1 |             56 |              int |          4 |        10 |     0 |         (null) |       (null) |             (null) |           (null) |         (null) |                       (null) |            (null) |                  (null) |                (null) |              (null) |               0 |                 0 |                        0 |        (null) |          (null) |        (null) |       (null) |        (null) |                  0 |                (null) |             0 |                  0 |                    0 |                   (null) |                 (null) |               (null) |          38 |          4 |           (null) |                (null) |

Maybe you want something like this? :-)

SELECT AMOUNT
FROM
(
SELECT COUNT(*) AS AMOUNT
FROM TEST
)X
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top