引用列的统计信息是否会阻止删除该列?
我正在尝试一个非常简单的 drop column
语句:
alter table MyTable drop column MyColumn
并收到几个错误
消息 5074,级别 16,状态 1,第 1 行
统计信息“_dta_stat_1268251623_3_2”取决于列“MyColumn”。
最终紧随其后的是
消息 4922,16 级,状态 9,第 1 行
ALTER TABLE DROP COLUMN MyColumn 失败,因为一个或多个对象访问此列。
我不认为统计数据会阻止删除列。他们有吗?如果是这样,由于这些显然是自动创建的统计信息,我不能依赖同一数据库的多个副本中相同的名称,那么我如何在要在不同数据库上执行的升级脚本中删除所有此类统计信息?
I'm trying a very simple drop column
statement:
alter table MyTable drop column MyColumn
and receiving several errors along the lines of
Msg 5074, Level 16, State 1, Line 1
The statistics '_dta_stat_1268251623_3_2' is dependent on column 'MyColumn'.
followed ultimately by
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE DROP COLUMN MyColumn failed because one or more objects access this column.
I didn't think statistics prevent a column from being dropped. Do they? If so, since these are apparently auto-created statistics I can't depend on the names being the same across multiple copies of the same database, so how could I drop all such statistics in an upgrade script to be executed on a different database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JNK 答案中提出的代码不起作用,但这个想法很好。如果您想删除所有用户创建的统计信息,这是我经过测试的解决方案:
The code proposed in JNK answer does not work, but the idea is good. If you want to delete all user created statistics this my tested solution :
我见过的自动生成的统计信息要么具有它们所代表的索引的名称,要么以诸如 WA_Sys_ 之类的内容开头。
您是否 100% 确定这不是某人设置的一组自定义统计数据?
检查一下:
...并查看
user_created
字段指示的内容。每条评论:
这尚未经过测试,但您可以尝试以下操作:
如果看起来不错,请将
PRINT
更改为EXEC
。sp_msforeachdb
是一个在后台的游标,但其余的逻辑您可以作为一个集合来执行。Auto-generated statistics that I have seen all either have the name of the index they represent OR start with something like
WA_Sys_
.Are you 100% sure this is not a set of custom stats someone set up?
Check this:
...and see what the
user_created
field indicates.Per comment:
This is untested but you could try something like:
Change the
PRINT
to anEXEC
if it looks good.sp_msforeachdb
is a cursor in the background but the rest of the logic you can do as a set.