如何检查 SQL Server 表中是否存在列
如果特定列不存在,我需要添加它。 我有类似以下内容,但它总是返回 false:
IF EXISTS(SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'myTableName'
AND COLUMN_NAME = 'myColumnName')
如何检查 SQL Server 数据库表中是否存在列?
I need to add a specific column if it does not exist. I have something like the following, but it always returns false:
IF EXISTS(SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'myTableName'
AND COLUMN_NAME = 'myColumnName')
How can I check if a column exists in a table of the SQL Server database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
SQL Server 2005 及以上:
Martin Smith 的版本更短:
SQL Server 2005 onwards:
Martin Smith's version is shorter:
更简洁的版本
关于查看元数据的权限这一点适用于所有答案,而不仅仅是这个答案。
请注意,第一个参数表名称为
COL_LENGTH
根据需要可以采用由一部分、两部分或三部分组成的名称格式。引用不同数据库中的表的一个示例是:
与使用元数据视图相比,此答案的一个区别是元数据函数(例如
COL_LENGTH
)始终仅返回有关已提交更改的数据,无论有效的隔离级别。A more concise version
The point about permissions on viewing metadata applies to all answers, not just this one.
Note that the first parameter table name to
COL_LENGTH
can be in one, two, or three part name format as required.An example referencing a table in a different database is:
One difference with this answer, compared to using the metadata views, is that metadata functions, such as
COL_LENGTH
, always only return data about committed changes, irrespective of the isolation level in effect.调整以下内容以满足您的具体要求:
这应该可行 - 仔细检查您的代码是否有愚蠢的错误; 例如,您是否在应用插入的同一数据库上查询 INFORMATION_SCHEMA ? 任一语句中的表/列名称是否有拼写错误?
Tweak the below to suit your specific requirements:
That should work - take a careful look over your code for stupid mistakes; are you querying INFORMATION_SCHEMA on the same database as your insert is being applied to for example? Do you have a typo in your table/column name in either statement?
尝试这个...
Try this...
对于在删除列之前检查列是否存在的人。
从SQL Server 2016开始,您可以使用新的DIE(Drop If Exists)语句而不是大型
IF
包装器For the people who are checking the column existence before dropping it.
From SQL Server 2016 you can use new DIE (Drop If Exists) statements instead of big
IF
wrappers我更喜欢
INFORMATION_SCHEMA.COLUMNS
而不是系统表,因为 Microsoft 不保证在版本之间保留系统表。 例如,dbo.syscolumns
在 SQL Server 2008 中仍然有效,但它已被弃用,并且将来可能随时删除。I'd prefer
INFORMATION_SCHEMA.COLUMNS
over a system table because Microsoft does not guarantee to preserve the system tables between versions. For example,dbo.syscolumns
does still work in SQL Server 2008, but it's deprecated and could be removed at any time in future.您可以使用信息模式系统视图来查找有关您感兴趣的表的几乎所有内容:
您还可以使用 Information_schema 视图查询视图、存储过程以及有关数据库的几乎所有内容。
You can use the information schema system views to find out pretty much anything about the tables you're interested in:
You can also interrogate views, stored procedures and pretty much anything about the database using the Information_schema views.
尝试类似的操作:
然后像这样使用它:
它应该适用于 SQL Server 2000和 SQL Server 2005。 我不确定 SQL Server 2008,但我不明白为什么不。
Try something like:
Then use it like this:
It should work on both SQL Server 2000 and SQL Server 2005. I am not sure about SQL Server 2008, but I don't see why not.
首先检查
table
/column
(id
/name
) 组合是否存在于dbo.syscolumns< /code> (包含字段定义的内部 SQL Server 表),如果没有,则发出相应的 ALTER TABLE 查询来添加它。 例如:
First check if the
table
/column
(id
/name
) combination exists indbo.syscolumns
(an internal SQL Server table that contains field definitions), and if not issue the appropriateALTER TABLE
query to add it. For example:我的一位好朋友兼同事向我展示了如何在 SQL Server 2005 及更高版本检查列。 您可以使用类似于以下内容的内容:
您可以在这里亲自查看:
A good friend and colleague of mine showed me how you can also use an
IF
block with SQL functionsOBJECT_ID
andCOLUMNPROPERTY
in SQL Server 2005 and later to check for a column. You can use something similar to the following:You can see for yourself here:
这在 SQL Server 2000 中对我有用:
This worked for me in SQL Server 2000:
尝试这个
Try this
最简单易懂的解决方案之一是:
One of the simplest and understandable solutions is:
我需要 SQL Server 2000 类似的东西,如 Mitch 指出,这仅适用于 SQL Server 2005 或更高版本。
这最终对我有用:
I needed something similar for SQL Server 2000 and, as Mitch points out, this only works in SQL Server 2005 or later.
This is what worked for me in the end:
如果列不存在,则执行某些操作:
如果列确实存在,则执行某些操作:
Do something if the column does not exist:
Do something if the column does exist:
接受的答案的临时表版本:
A temporary table version of the accepted answer:
有多种方法可以检查列是否存在。
我强烈建议使用
INFORMATION_SCHEMA.COLUMNS
因为它是为了与用户通信而创建的。考虑下表:
甚至还有一些其他访问方法可用于检查
系统目录
。此外,无需使用
SELECT *
,只需通过NULL值
进行测试即可代码>There are several ways to check the existence of a column.
I would strongly recommend to use
INFORMATION_SCHEMA.COLUMNS
as it is created in order to communicate with user.Consider following tables:
and even some other access methods available to check
system catalog.
Also, no need to use
SELECT *
, simply test it byNULL value
小麦的答案 很好,但它假设您在任何模式或数据库中都没有任何相同的表名/列名对。 为了使其在这种情况下安全,请使用此...
Wheat's answer is good, but it assumes you do not have any identical table name / column name pairs in any schema or database. To make it safe for that condition, use this...
另一个贡献是以下示例,如果该列不存在,则添加该列。
Another contribution is the following sample that adds the column if it does not exist.
下面是我用来管理数据库中列添加的一个简单脚本:
在此示例中,
Name
是要添加的ColumnName
,Object_Id
> 是表名
Here is a simple script I use to manage addition of columns in the database:
In this example, the
Name
is theColumnName
to be added andObject_Id
is theTableName
下面的查询可用于检查表中是否存在搜索列。 我们可以根据搜索结果做出决定,也如下所示。
The below query can be used to check whether searched column exists or not in the table. We can take a decision based on the searched result, also as shown below.
还有另一种变化...
Yet another variation...
您可以一次检查 SQLDB 中的多个列,并返回一个字符串作为状态来检查列是否存在:
You can check multiple columns in SQLDB at once and return a string as status to check if columns exist:
INFORMATION_SCHEMA 使用当前数据库,您必须首先调用“Use [MyDatabase]”
INFORMATION_SCHEMA work with current database, you must first call "Use [MyDatabase]"
通过查询从外部程序获取结果的一种简单方法是:
A simply way to get a result from external program with a query is that:
执行以下查询来检查给定表中是否存在该列:
Execute the below query to check if the column exists in the given table: