删除字段出现错误?

Ivan_pan 2004-05-08 09:36:25
如果某字段存在关系、索引/键或者约束时,删除该字段就会出错,请问怎样取得该字段的所有关系、索引/键或者约束,然后把他们删除,再把该字段删除?在线等待,很急!!!!
...全文
121 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ivan_pan 2004-05-08
  • 打赏
  • 举报
回复
谢谢邹建兄,问题解决了!分全给你了!
Ivan_pan 2004-05-08
  • 打赏
  • 举报
回复
执行alter table 表 drop column 字段 后出现错误
服务器: 消息 5074,级别 16,状态 8,行 31
对象 'PK_ken' 依赖于 列 'pkid'。
服务器: 消息 4922,级别 16,状态 1,行 31
ALTER TABLE DROP COLUMN pkid 失败,因为有一个或多个对象访问此列。
Ivan_pan 2004-05-08
  • 打赏
  • 举报
回复
运行后出现:对于 DROP INDEX,必须以 tablename.indexname 的形式同时给出表名和索引名。请问怎么回事?
zjcxc 2004-05-08
  • 打赏
  • 举报
回复
注意修改上述语句中的"表名"为你要处理的表名
字段名改为你要删除的字段名.

执行完上述语句后,再执行删除字段的语句:
alter table 表 drop column 字段
zjcxc 2004-05-08
  • 打赏
  • 举报
回复
--删除指定表指定字段的所有关系

declare tb cursor local for
--默认值约束
select sql='alter table ['+b.name+'] drop constraint ['+d.name+']'
from syscolumns a
join sysobjects b on a.id=b.id
join syscomments c on a.cdefault=c.id
join sysobjects d on c.id=d.id
where b.name='表名'
and a.name='字段名'
union all --索引
select 'drop index ['+a.name+']'
from sysindexes a
join sysindexkeys b on a.id=b.id and a.indid=b.indid
join sysobjects c on b.id=c.id and c.xtype='U' and c.name<>'dtproperties'
join syscolumns d on b.id=d.id and b.colid=d.colid
where a.indid not in(0,255)
and c.name='表名'
and d.name='字段名'
union all --外键引用
select s='alter table ['+c.name+'] drop constraint ['+b.name+']'
from sysforeignkeys a
join sysobjects b on b.id=a.constid
join sysobjects c on c.id=a.fkeyid
join syscolumns d on d.id=c.id and a.fkey=d.colid
join sysobjects e on e.id=a.rkeyid
join syscolumns f on f.id=e.id and a.rkey=f.colid
where e.name='表名'
and d.name='字段名'

declare @s varchar(8000)
open tb
fetch next from tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch next from tb into @s
end
close tb
deallocate tb
zjcxc 2004-05-08
  • 打赏
  • 举报
回复
--删除索引的语句搞错了,重试下面的这个:

--删除某字段的所有关系

declare tb cursor local for
--默认值约束
select sql='alter table ['+b.name+'] drop constraint ['+d.name+']'
from syscolumns a
join sysobjects b on a.id=b.id
join syscomments c on a.cdefault=c.id
join sysobjects d on c.id=d.id
where b.name='表名'
and a.name='字段名'
union all --外键引用
select s='alter table ['+c.name+'] drop constraint ['+b.name+']'
from sysforeignkeys a
join sysobjects b on b.id=a.constid
join sysobjects c on c.id=a.fkeyid
join syscolumns d on d.id=c.id and a.fkey=d.colid
join sysobjects e on e.id=a.rkeyid
join syscolumns f on f.id=e.id and a.rkey=f.colid
where e.name='表名'
and d.name='字段名'
union all --索引
select case e.xtype when 'PK' then 'alter table ['+c.name+'] drop constraint ['+e.name+']'
else 'drop index ['+c.name+'].['+a.name+']' end
from sysindexes a
join sysindexkeys b on a.id=b.id and a.indid=b.indid
join sysobjects c on b.id=c.id and c.xtype='U' and c.name<>'dtproperties'
join syscolumns d on b.id=d.id and b.colid=d.colid
join sysobjects e on c.id=e.parent_obj
where a.indid not in(0,255)
and c.name='a'
and d.name='id'

declare @s varchar(8000)
open tb
fetch next from tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch next from tb into @s
end
close tb
deallocate tb



Ivan_pan 2004-05-08
  • 打赏
  • 举报
回复
还是有问题:
服务器: 消息 3723,级别 16,状态 4,行 1
不允许对索引 'ken.PK_ken' 显式地使用 DROP INDEX。该索引正用于 PRIMARY KEY 约束的强制执行。
邹建兄麻烦再改一下。在线等待!拜托
zjcxc 2004-05-08
  • 打赏
  • 举报
回复
--删除索引的语句搞错了,重试下面的这个:

--删除某字段的所有关系

declare tb cursor local for
--默认值约束
select sql='alter table ['+b.name+'] drop constraint ['+d.name+']'
from syscolumns a
join sysobjects b on a.id=b.id
join syscomments c on a.cdefault=c.id
join sysobjects d on c.id=d.id
where b.name='表名'
and a.name='字段名'
union all --外键引用
select s='alter table ['+c.name+'] drop constraint ['+b.name+']'
from sysforeignkeys a
join sysobjects b on b.id=a.constid
join sysobjects c on c.id=a.fkeyid
join syscolumns d on d.id=c.id and a.fkey=d.colid
join sysobjects e on e.id=a.rkeyid
join syscolumns f on f.id=e.id and a.rkey=f.colid
where e.name='表名'
and d.name='字段名'
union all --索引
select 'drop index ['+c.name+'].['+a.name+']'
from sysindexes a
join sysindexkeys b on a.id=b.id and a.indid=b.indid
join sysobjects c on b.id=c.id and c.xtype='U' and c.name<>'dtproperties'
join syscolumns d on b.id=d.id and b.colid=d.colid
where a.indid not in(0,255)
and c.name='表名'
and d.name='字段名'

declare @s varchar(8000)
open tb
fetch next from tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch next from tb into @s
end
close tb
deallocate tb

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧