Guzzyman
Guzzyman

Reputation: 561

Returning only the column names from a SELECT statement

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!

Upvotes: 3

Views: 8070

Answers (7)

Sagar Rawal
Sagar Rawal

Reputation: 1442

Try following SQL statement to get column name.

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

Upvotes: 0

Teemu Lehto
Teemu Lehto

Reputation: 1

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

Upvotes: 0

Karan Kular
Karan Kular

Reputation: 9

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

Upvotes: 0

Ramesh Rajendran
Ramesh Rajendran

Reputation: 38663

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

Upvotes: 0

Manoj Mevada
Manoj Mevada

Reputation: 659

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

Upvotes: 0

Sandip Bantawa
Sandip Bantawa

Reputation: 2880

SET FMTONLY ON
SELECT * FROM tablename

Upvotes: 1

MichaC
MichaC

Reputation: 13381

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

Should do it

Upvotes: 5

Related Questions