简体   繁体   English

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

[英]How to check whether the table contains a particular column or not?

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

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

You can query the information schema tables for this kind of information and much more. 您可以查询信息模式表以获得此类信息以及更多信息。

In your case something like this would be useful: 在您的情况下,如下所示将很有用:

select
* 
from
INFORMATION_SCHEMA.COLUMNS
where
    table_schema = '<your schema>'
    and
    table_name = '<your table>'
if exists
(select * from sys.columns
 where Name = N'columnName' and Object_ID = Object_ID(N'tableName'))

Since you are looking for a particular column. 由于您正在寻找特定的列。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL:检查表是否在特定列中不包含任何空值 - SQL: check if a table contains no nulls in a particular column 如何检查子表是否包含特定记录 - How to check if a child table contains a particular record 如何检查特定的列引用父表 - How to check a particular column references parent table 如何检查字符串是否与包含多个相似模式的字符串中的特定相似模式匹配 - how to check whether a string matches a particular like pattern in a string which contains multiple like patterns 如何检查表中是否存在列? - How do I check whether column exists in the table? 如何检查/选择字符串是否包含表列值中的值? - How to check/select if a string contains value from a table column values? 检查每一行和每一列中是否存在特定数据的有效方法? - Effective way to check whether particular data exist in each row and column or not? 检查我的表中是否有特定的名称顺序 - Check whether particular name order is available in my table MYSQL:向结果添加一列,其中包含此行是否包含特定列的最大数量 - MYSQL: Add a column to result which contains whether this rows contain max number of a particular column or not MySQL脚本在添加表中的列之前检查列是否存在 - Mysql script check whether column exist or not before adding column in table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM