I want to query the name of all columns of a table. I found how to do this in:
But I also need to know:
It will check whether the given the table
is Base Table.
SELECT
T.TABLE_NAME AS 'TABLE NAME',
C.COLUMN_NAME AS 'COLUMN NAME'
FROM INFORMATION_SCHEMA.TABLES T
INNER JOIN INFORMATION_SCHEMA.COLUMNS C ON T.TABLE_NAME=C.TABLE_NAME
WHERE T.TABLE_TYPE='BASE TABLE'
AND T.TABLE_NAME LIKE 'Your Table Name'
You can write this query to get column name and all details without using INFORMATION_SCHEMA in MySql :
SHOW COLUMNS FROM database_Name.table_name;
This SO question is missing the following approach :
-- List down all columns of table 'Logging'
select * from sys.all_columns where object_id = OBJECT_ID('Logging')
you can use this query
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME like N'%[ColumnName]%' and TABLE_NAME = N'[TableName]'
One other option which is arguably more intuitive is:
SELECT [name]
FROM sys.columns
WHERE object_id = OBJECT_ID('[yourSchemaType].[yourTableName]')
This gives you all your column names in a single column.
If you care about other metadata, you can change edit the SELECT STATEMENT TO SELECT *
.
SELECT column_name, data_type, character_maximum_length, table_name,ordinal_position, is_nullable
FROM information_schema.COLUMNS WHERE table_name LIKE 'YOUR_TABLE_NAME'
ORDER BY ordinal_position