How to check whether column exist or not in a table?

Discussion in 'Databases' started by Tintumol, Dec 23, 2010.

  1. #1
    Hi,
    I want to check whether a particular column exist or not in a given table. This i want to check before dropping the column so no error comes if column is not present.

    So please can anybody tell me how i do this?
    Exmple:
    --How to do: check if column exist
    -- if exist
    ALTER table table_name DROP COLUMN column_name
     
    Tintumol, Dec 23, 2010 IP
  2. haa

    haa Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Next time search the web before posting here :)
    
    IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = ‘TEST’ AND COLUMN_NAME = ‘TEST_DATE’)
    BEGIN
       ALTER TABLE TEST ADD TEST_DATE DATETIME
    END
    
    Code (markup):
     
    haa, Dec 23, 2010 IP