Reputation: 561
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
Reputation: 1442
Try following SQL statement to get column name.
SELECT column_name
FROM information_schema.columns
WHERE TABLE_NAME='tableName'
Upvotes: 0
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
Reputation: 9
select column_name,* from information_schema.columns where table_name = 'YourTableName' order by ordinal_position
Upvotes: 0
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
Referred from : How can I get column names from a table in Oracle?
and another one way
Upvotes: 0
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
Reputation: 13381
SELECT COLUMN_NAME,*
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'tableName' AND TABLE_SCHEMA='dbo'
Should do it
Upvotes: 5