将列添加到表中(如果尚不存在)
我想为 MS SQL Server 编写一个查询,将列添加到表中。但当我运行/执行以下查询时,我不希望显示任何错误。
我正在使用这种查询来添加表...
IF EXISTS (
SELECT *
FROM sys.objects
WHERE OBJECT_ID = OBJECT_ID(N'[dbo].[Person]')
AND TYPE IN (N'U')
)
但我不知道如何为列编写此查询。
I want to write a query for MS SQL Server that adds a column into a table. But I don't want any error display, when I run/execute the following query.
I am using this sort of query to add a table ...
IF EXISTS (
SELECT *
FROM sys.objects
WHERE OBJECT_ID = OBJECT_ID(N'[dbo].[Person]')
AND TYPE IN (N'U')
)
But I don't know how to write this query for a column.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以通过使用
sys.columns
表 iosys.objects
来使用类似的构造。You can use a similar construct by using the
sys.columns
table iosys.objects
.另一种选择。我更喜欢这种方法,因为它的写作量较少,但两者完成了相同的事情。
我还注意到你正在寻找桌子确实存在的地方,显然就是这个
Another alternative. I prefer this approach because it is less writing but the two accomplish the same thing.
I also noticed yours is looking for where table does exist that is obviously just this
这是对我有用的另一种变体。
IF NOT EXISTS (SELECT 1
来自 SYS.COLUMNS...
Here's another variation that worked for me.
IF NOT EXISTS (SELECT 1
FROM SYS.COLUMNS....
我希望这会有所帮助。 更多信息
I Hope this would help. More info
当检查另一个数据库中的列时,您可以简单地包含数据库名称:
When checking for a column in another database, you can simply include the database name: