Question

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.

Was it helpful?

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'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top