Check column in table

How to check if a column exists in a table using SQL query? I am using Access 2007.

+3
source share
3 answers

You can use Information_schema views:

If Not Exists (Select Column_Name
               From INFORMATION_SCHEMA.COLUMNS
               Where Table_Name = 'YourTable'
               And Column_Name = 'YourColumn')
begin

-- Column doesn't exist

end

In addition, you can restrict the offer whereby including a database and / or schema in it.

If Not Exists (Select Column_Name
               From INFORMATION_SCHEMA.COLUMNS
               Where Table_Name = 'YourTable'
               And Column_Name = 'YourColumn'
               And Table_Catalog = 'YourDatabaseName'
               And Table_Schema = 'YourSchemaName')

begin

-- Column doesn't exist

end
+4
source
if Exists(select * from sys.columns where Name = N'columnName'  
            and Object_ID = Object_ID(N'tableName'))

begin

    -- Column Exists

end

"LINK"

+2
source
IF NOT EXISTS (SELECT 1
FROM syscolumns sc
JOIN sysobjects so
ON sc.id = so.id
WHERE so.Name = 'TableName'
AND sc.Name = 'ColumnName')
BEGIN
--- do your stuff
END
+2
source

Source: https://habr.com/ru/post/1783813/


All Articles