如何检查表是否包含特定列?

时间:2015-06-04 13:47:29

标签: sql sql-server

如何检查表是否包含特定列?

4 个答案:

答案 0 :(得分:3)

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'table_name' AND COLUMN_NAME = 'column_name'

答案 1 :(得分:2)

您可以查询信息架构表以获取此类信息等等。

在你的情况下,这样的事情会很有用:

select
* 
from
INFORMATION_SCHEMA.COLUMNS
where
    table_schema = '<your schema>'
    and
    table_name = '<your table>'

答案 2 :(得分:1)

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

答案 3 :(得分:1)

因为您正在寻找特定的专栏。

IF EXISTS(
SELECT TOP 1 *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE [TABLE_NAME] = 'TableName'
AND [COLUMN_NAME] = 'ColumnName'
AND [TABLE_SCHEMA] = 'SchemaName')
BEGIN
PRINT 'Your Column Exists'
END