3

This is just a lazy thought. I have a table with about 9 columns names. Is there any way I can use SQL statement to return only the Column names? The normal way is to write out my column names from

SELECT * FROM tableName; 

statement but was wondering if I can get the column names with SQL statement.

Any Ideas would be appreciated.

Thanks You!

9
  • 1
    I suppose that this is a request for Sql Server right? Commented Dec 31, 2013 at 11:10
  • 1
    What DBMS are you using? If it is SQL Server I would be inclined to use the system views (e.g. sys.columns) rather than the information schema for reasons outlined in this article by Aaron Bertrand Commented Dec 31, 2013 at 11:11
  • Hi Bro, this statements would be used via the code behind file in an ASP.NET application. I need to get the result set then parse the returned values to a dropDownList control which will then be used for further quesries. So in a nutshell...would be using SQL Server when the application is finally deployed to the life Server. Commented Dec 31, 2013 at 11:17
  • possible duplicate of How can I get column names from a table? Commented Dec 31, 2013 at 11:27
  • @RameshRajendran that question is about Oracle Commented Dec 31, 2013 at 12:10

7 Answers 7

5
SELECT COLUMN_NAME,* 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'tableName' AND TABLE_SCHEMA='dbo'

Should do it

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

Comments

1
SET FMTONLY ON
SELECT * FROM tablename

Comments

0

Open you Microsoft SQL Server Management Studio, in New Query, Just type your Table name and select it and then press ALT + F1 Key, it will give all details about table

Comments

0

You can query the syscolumns table for table column metadata.

 SELECT [name] AS [Column Name]
FROM syscolumns
WHERE id = (SELECT id FROM sysobjects WHERE  [Name] = 'TableName')

See this image that query return values

enter image description here

Referred from : How can I get column names from a table in Oracle?

and another one way

1 Comment

Thanks, I tried this method alongside with the others in the answers to my question but I got only the COLUMN NAME header as my result. So decided to use a datatable to get the result of the normal sql SELECT * FROM tableName statement, binded it to a gridView, used the HeaderRow.Count property of the GridView to get the HeaderNames, passed same into a DataTable, then Populated my Drop Down List Control with the results.
0

select column_name,* from information_schema.columns where table_name = 'YourTableName' order by ordinal_position

Comments

0

Using SQL Server xml raw you get the column names from any sql query. Replace 'select top 1 * from [TABLENAME]' from the example below. Query must return at least 1 row of data and remember to use top 1.

declare @t1 table (x xml)
declare @t2 table (x xml)
declare @t3 table (x xml)

insert into @t1 select cast( (select top 1 * from [TABLENAME] for xml raw) as xml)
insert into @t2 select y.r.query('.') from @t1 cross apply x.nodes('/row') as y(r)
insert into @t3 select t2.n.value('local-name(.)', 'varchar(max)') from @t2 t cross apply x.nodes('//@*') as t2(n)

select replace (convert (nvarchar, x),'_x0020_', ' ') from @t3

Comments

0

Try following SQL statement to get column name.

SELECT column_name   
FROM information_schema.columns 
WHERE TABLE_NAME='tableName'

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.