Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 634 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I get column names from a table in SQL Server?

#1
I want to query the name of all columns of a table. I found how to do this in:

- [Oracle](

[To see links please register here]

)
- [MySQL](

[To see links please register here]

)
- [PostgreSQL](

[To see links please register here]

)

But I also need to know: **how can this be done in *Microsoft SQL Server* (2008 in my case)?**

Reply

#2
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

Reply

#3
It will check whether the given the `table` is **[Base Table](

[To see links please register here]

)**.


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'
Reply

#4
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')

Reply

#5
By using this query you get the answer:

select Column_name
from Information_schema.columns
where Table_name like 'table name'
Reply

#6
select *
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='tableName'

This is better than getting from `sys.columns` because it shows `DATA_TYPE` directly.
Reply

#7
You can try this.This gives all the column names with their respective data types.

desc <TABLE NAME> ;
Reply

#8
you can use this query

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME like N'%[ColumnName]%' and TABLE_NAME = N'[TableName]'
Reply

#9
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;

Reply

#10
SELECT c.Name
FROM sys.columns c
JOIN sys.objects o ON o.object_id = c.object_id
WHERE o.object_id = OBJECT_ID('TABLE_NAME')
ORDER BY c.Name
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through