How can I get column names from a table in SQL Server?
You can obtain this information and much, much more by querying the Information Schema views.
This sample query:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'Customers'
Can be made over all these DB objects:
- CHECK_CONSTRAINTS
- COLUMN_DOMAIN_USAGE
- COLUMN_PRIVILEGES
- COLUMNS
- CONSTRAINT_COLUMN_USAGE
- CONSTRAINT_TABLE_USAGE
- DOMAIN_CONSTRAINTS
- DOMAINS
- KEY_COLUMN_USAGE
- PARAMETERS
- REFERENTIAL_CONSTRAINTS
- ROUTINES
- ROUTINE_COLUMNS
- SCHEMATA
- TABLE_CONSTRAINTS
- TABLE_PRIVILEGES
- TABLES
- VIEW_COLUMN_USAGE
- VIEW_TABLE_USAGE
- VIEWS
How can I get column names from a table in SQL?
MYSQL, MS SQL and Postgresql (thanks @christophe)
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_SCHEMA = 'database_name'
AND TABLE_NAME = 'table_name'
PIVOT the results if you need column names in one line
How do you return the column names of a table?
Not sure if there is an easier way in 2008 version.
USE [Database Name]
SELECT COLUMN_NAME,*
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'YourTableName' AND TABLE_SCHEMA='YourSchemaName'
Get column names of all tables in SQL
You can use INFORMATION_SCHEMA.COLUMNS
:
select c.*
from INFORMATION_SCHEMA.COLUMNS c;
This has name, type, and a lot of other information for all tables in a database -- note, not on a server but in a database.
Find the column names of Temp table
SELECT *
FROM tempdb.sys.columns
WHERE object_id = Object_id('tempdb..#sometemptable');
Related Topics
Count(*) Vs. Count(1) Vs. Count(Pk): Which Is Better
Error in MySQL When Setting Default Value For Date or Datetime
In VS or in the SQL Where Clause
How to Select a Column Name With a Space in MySQL
Convert Utf-8 String Classic Asp to SQL Database
Oracle Differences Between Nvl and Coalesce
How to Create a Table from Select Query Result in SQL Server 2008
Passing Multiple Values For a Single Parameter in Reporting Services
Is There an Oracle SQL Query That Aggregates Multiple Rows into One Row
Convert Datetime Column from Utc to Local Time in Select Statement
How to Ignore Ampersands in a SQL Script Running from SQL Plus
Grouping into Interval of 5 Minutes Within a Time Range
Multiple Insert Statements Vs. Single Insert With Multiple Values
Limit on the Where Col in (...) Condition