开发者

Check Column Exist in a Table [duplicate]

开发者 https://www.devze.com 2023-02-02 13:11 出处:网络
This question already ha开发者_JAVA技巧s answers here: How to check if specific column exists in an Access database table
This question already ha开发者_JAVA技巧s answers here: How to check if specific column exists in an Access database table (6 answers) Closed 3 years ago.

How do I check if a column exists in a table using a SQL query? I'm using Access 2007.


You can use the 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 may want to restrict the where clause further by including the Database and/or schema.

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


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

begin

    -- Column Exists

end

"REFERENCE"


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
0

精彩评论

暂无评论...
验证码 换一张
取 消