3

I am wondering if there is a way to write the SQL so that it would return me a result as usual but now, on the first row that would return also the attribute names.

To explain what I mean:

say you have a table "test" which has 2 attributes "id" and "name":

id name
1  nik
2  tst

query:

SELECT * FROM test; 

produces:

1 nik
2 tst

but what I want it to return is this:

id name
1 nik
2 tst

Is this possible?

edit: I am using PostreSQL

4
  • 2
    Why do you want to do this? It seems completely redundant, but maybe your explanation of why would help us see what you want. Commented Aug 31, 2011 at 14:06
  • 4
    What you're after is meta data, and is generally accessible via the jar/driver your software is using to interface with the database. It helps to know the database, if you want a specific answer ;) Commented Aug 31, 2011 at 14:08
  • No general solution exists for this. You'd have to query the data dictionary or system catalog for this. Commented Aug 31, 2011 at 14:09
  • Do you use a particular GUI? What tool are you using to generate your results? Commented Aug 31, 2011 at 14:37

5 Answers 5

4

You cannot return the names and the actual column values in a single result unless you give up on the real datatypes (which is probably not what you want).

Your example mixes character data and numeric data in the id column and Postgres will (rightfully) refuse to return such a result set.

Edit:
I tested the "union" solution given e.g. by JNK and it fails (as expected) on Postgres, Oracle and SQL Server precisely because of the non-matching datatypes. MySQL follows it's usual habits of not throwing errors and simply converts everything to characters.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 - No idea why this was downvoted. Sometimes the answer is "You can't/shouldn't".
@JNK: correct! I shouldn't do this, I was being lazy, which eventually would bring me trouble (because of the mentioned non-matching types - even though you can use in postgres sth like SELECT id::text FROM table; and you would not get this error - but nevertheless it's wrong to do it this way.)
2

Extremely generic answer since you don't provide an RDBMS:

SELECT id, name FROM(
SELECT 'id' as 'id', 'name' as 'name', 1 as 'Rank'
UNION ALL
SELECT *, 2 as 'Rank' FROM test) as X
ORDER BY [RANK]

EDIT

Thanks to Martin for pointing out the need for the ORDER BY

1 Comment

This will not work if the table contains non-character columns. The union requires that the columns of each "union part" have matching datatypes. Because of the first select (that returns the names) all subsequent columns must be character columns as well. Any database that does a decent data type check (e.g. Postgres, Oracle) will refuse to run that because the ID column is a numeric one.
1

Assuming you are on SQL Server, you can get the column names of a specific table by using this query:

select column_name 'Column Name', data_type 'Data Type'
from information_schema.columns
where table_name = 'putYourTableNameHere'

Then, you'll have to UNION your things together.

2 Comments

Use of information_schema to fetch column names is interesting idea, however note that above query actually returns multiple rows, so you also need to additionally construct pivot table (that is, with multiple columns) for UNION.
Yeah, but by shooting into the blue without knowing which DBMS he's on, I did not feel like elaborating this in deep.
1

Depending on your tools / technique, but here are a couple:

  1. If you're using SSMS (Sql-Server), and want to copy/paste your results with the column headers:

    • Query Window -->
    • R-Click
    • Results -->
    • Grid or Text -->
    • Check-mark the 'Include column headers in the result set' option

  2. If you're using Sql-Server, you can query meta-tables (sys.columns, etc.)

  3. If you're using an ASP.NET databound control, you usually have access to methods or properties (sqldatareader.getname(i), etc.)

Anyway -- just depends on the layer you're trying to get the names from -- if these above don't help, then edit / re-tag your question so we can focus on whatever tool you're wanting to use to do this.

EDIT for PostgresSQL

  1. If you're using PostgresSQL, you can query meta-tables (information_schema.columns, etc.)

Comments

0

I agree with OMG Ponies above, the way to get this meta-data is usually from the interface you use.

for example the Perl DBI module has a method fetchrow_hashref where the columns of the returned row are returned as an associative array (hash) where the colnames are the keys.

print $ref->{'name'}; # would print nik or tst

Update: I had forgotten to add that some of these interface layers have a method that return s the col names and you could use that instead of adding the names into your result set. The DBI method you'd use would be $sth->{NAMES}->[0] would return the first column name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.