Ramani Sandeep's Blog

DotNetting – Fast , Easy Way of Developing Applications

Query to Find Column Name From All Tables

Posted by Ramani Sandeep on April 7, 2009

Question : How many tables in your database have column name like  ‘CategoryID’  ?

Solution :

SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE ‘%CategoryID%’
ORDER BY schema_name, table_name;

Question : How to find all the column name from your database ?

Solution :

SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
ORDER BY schema_name, table_name;

Hope this will help

Jay Ganesh

Advertisement

3 Responses to “Query to Find Column Name From All Tables”

  1. Martin said

    When and where were you born?

  2. Krishna Patel said

    Hi Mr.Sandeep,

    how to wright under give Sql Query in Linq ???

    select coalesce(Clientname,’,') + ‘-’ + coalesce(clienturl,’,') as HostHeaders, ClientName, ClientURL, Id from Client

    • Ramani Sandeep said

      Hi Krishna,

      The ?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types.

      your Query can be written as :

      Var myClient = from c in DBContext.Client
      select new
      {
      HostHeaders = c.ClientName ?? ‘,’ + ‘-’ + c.ClientUrl ?? ‘,’
      };

      Please check this out and let me know if you still face any issue.

      Regards,
      Sandeep

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 37 other followers