Search code examples
sqlitedata-dictionary

How do you tell if a particular column is in a table?


I would like to know what query to use to determine if a particular table column exists in a given table.

For example I would like to know if the column named Address_1 exists in the table named Visits.

I think it is in a data dictionary table maybe.


Solution

  • Edit: As pointed out by @tidwall, this answer is for SQL Server and won't work for sqlite. This stackoverflow thread has the right answer for sqlite.


    SELECT name
    FROM sysobjects
    WHERE id IN (
        SELECT id
        FROM syscolumns
        WHERE name = 'THE_COLUMN_NAME'
    )
    

    also, this yields all the table column information for your parsing enjoyment:

    SELECT COLUMN_NAME, data_type, character_maximum_length
    FROM information_schema.columns
    WHERE TABLE_NAME = 'your_table_name'