如何使用SQL查询检查表中是否存在列?我正在使用Access 2007。
答案 0 :(得分:4)
您可以使用Information_schema次观看次数:
If Not Exists (Select Column_Name
From INFORMATION_SCHEMA.COLUMNS
Where Table_Name = 'YourTable'
And Column_Name = 'YourColumn')
begin
-- Column doesn't exist
end
此外,您可能希望通过包含数据库和/或架构来进一步限制where
子句。
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
答案 1 :(得分:2)
if Exists(select * from sys.columns where Name = N'columnName'
and Object_ID = Object_ID(N'tableName'))
begin
-- Column Exists
end
答案 2 :(得分:2)
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