Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

Phagu Mahato
More...


Resources » Code Snippets » SQL DBA

Get Info about table columns


Posted Date:     Category: SQL DBA    
Author: Member Level: Gold    Points: 10


The following snippet will help the developers to get the column info of a particular table or database. Often when we work with the database we may need to get the total number of columns in the table and also when we do sort of analysis on any table then we need to get the complete tables/coumns information of a schema.database. These queries will help to do so.



 


Hi,
Developers may need to get the info on database tables. Here is a code that helps the developers to get the count of columns of a table.

SELECT COUNT(*)
FROM yourdatabase.sys.columns
WHERE object_id = OBJECT_ID('yourdatabase.dbo.tablename')


Example :

SELECT COUNT(*)
FROM eVAT.sys.columns
WHERE object_id = OBJECT_ID('eVAT.dbo.Registration')


Now the developers may also need to get the name of columns of a particular table.


SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG='databasename' AND TABLE_NAME='tablename'


Example:

SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG='eVAT' AND TABLE_NAME='Registration'


If anyone needs complete info of all the tables in a database then try this


SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG='databasename'


Example:


SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG='eVAT'


Executing the above code without the 'TABLE_CATELOG' will list all the database table's information in the schema.


SELECT *
FROM INFORMATION_SCHEMA.COLUMNS


Example:

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS


Hope it may help all.





Did you like this resource? Share it with your friends and show your love!


Responses to "Get Info about table columns"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: List all the disabled and currently Running Sql Agent JOBS
    Return to Resources
    Post New Resource
    Category: SQL DBA


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Object Info  .  Count Coulmns  .  Column info  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.