Finding a Column in Database Table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pawanez4u
    New Member
    • Sep 2007
    • 10

    #1

    Finding a Column in Database Table

    Hi Every one,

    I want to find whether a particular column in a SQL database table exists or not which the "column name".

    Kindly help me with the appropriate.

    Thanks in Advance.

    Regards
    Pavankumar P
  • Torgg
    New Member
    • Dec 2007
    • 41

    #2
    Since you did not state what language you were using I'll skip the code samples. I found this link and it gives you a SELECT statement that returns all the column names. I would use the SELECT statement to populate a datatable and then I would search the datatable for the Column Name you want. You could also modify the query to select only the column name and table you are looking for, if it returns nothing then the column was not found.



    Code:
    SELECT
    COLUMN_NAME,
    DATA_TYPE,
    CHARACTER_MAXIMUM_LENGTH
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = 'Customers'
    OR

    Code:
    SELECT
    COLUMN_NAME
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE COLUMN_NAME = 'YourColumnName' AND TABLE_NAME = 'Customers'

    I hope this was helpfull,
    Torgg

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by Torgg
      Since you did not state what language you were using I'll skip the code samples. I found this link and it gives you a SELECT statement that returns all the column names. I would use the SELECT statement to populate a datatable and then I would search the datatable for the Column Name you want. You could also modify the query to select only the column name and table you are looking for, if it returns nothing then the column was not found.



      Code:
      SELECT
      COLUMN_NAME,
      DATA_TYPE,
      CHARACTER_MAXIMUM_LENGTH
      FROM INFORMATION_SCHEMA.COLUMNS
      WHERE TABLE_NAME = 'Customers'
      OR

      Code:
      SELECT
      COLUMN_NAME
      FROM INFORMATION_SCHEMA.COLUMNS
      WHERE COLUMN_NAME = 'YourColumnName' AND TABLE_NAME = 'Customers'

      I hope this was helpfull,
      Torgg
      That would work for the MySQL database. It would also help if the OP specified which database they were using.

      Comment

      Working...