SQL Server 2000 删除带有约束的列
我遇到了与这个问题中描述的相同的问题,但就是这样SQL Server 2005 和“已接受”的答案在 SQL Server 2000 中不起作用。
具体来说:我正在尝试运行 ALTER TABLE foo DROP COLUMN bar,但它失败了,因为存在“默认约束”。 ” 这意味着,我在该列上有一个默认值,SQL Server 将其实现为一个单独的约束,我需要先将其删除。
问题是创建列时没有为默认约束指定名称,因此我必须查询系统表以发现约束的(自动生成的)名称。
其他问题中给出的答案适用于 SQL Server 2005,但不适用于 SQL Server 2000。我需要后者。
[更新]我需要一个查询来回答“表foo
中列bar
的默认约束的名称是什么”这个问题。 这不是人类手动寻找答案的方法。
I have the same problem as described in this question, but there it's SQL Server 2005 and the "accepted" answer doesn't work in SQL Server 2000.
Specifically: I'm trying to run ALTER TABLE foo DROP COLUMN bar
, and it's failing because there's a "default constraint." Meaning, I have a default value on that column which SQL Server implements as a separate constraint that I need to delete first.
The problem is no name was given for the default constraint when the column was created, so I have to query the system tables to discover the (auto-generated) name of the constraint.
The answer given in that other question works for me in SQL Server 2005 but not in SQL Server 2000. I need the latter.
[UPDATE] I need a query that can answer the question "What is the name of the default constraint for column bar
in table foo
." Not a way for a human being to manually find the answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
刚刚弄清楚引用的 SQL 2005 查询实际上在做什么。 下面是在 SQL 2000 中运行的该查询的复制
Just figured out what the referenced SQL 2005 query was actually doing. Here's a replication of that query that works in SQL 2000
要获取表的默认约束列表,请尝试以下操作
To get a list of the default constraints for a table, try this
为您提供有关表的一堆信息 - 包括所有约束和约束名称
实际上 sp_help 调用此:
默认约束将显示“列 xxx 上的默认值”
gives you a bunch of info on the table - including all the constraints and the constraint names
Actually sp_help calls this:
Default constraints will say "DEFAULT on column xxx"
这个是在 bdukes 提供的第一个解决方案的基础上构建的,与 information_schema.columns 混合,似乎 info 包含默认所属列的序号位置
This one is constructed over the first solution provided by bdukes, mixed with information_schema.columns, it seems that info contains the ordinal position of the column the default belongs