sql server删除主表的一条数据,外键表的数据全没了
2015-12-08
展开全部
--使用SYSREFERENCES
--删除外键
set xact_abort on
begin tran
DECLARE @SQL VARCHAR(99)
DECLARE CUR_FK CURSOR LOCAL FOR
SELECT 'alter table ['+ OBJECT_NAME(FKEYID) + '] drop constraint ' + OBJECT_NAME(CONSTID) FROM SYSREFERENCES
--删除所有外键
OPEN CUR_FK
FETCH CUR_FK INTO @SQL
WHILE @@FETCH_STATUS =0
BEGIN
EXEC(@SQL)
FETCH CUR_FK INTO @SQL
END
CLOSE CUR_FK
DEALLOCATE CUR_FK
-- 删除所有表
DECLARE CUR_TAB CURSOR LOCAL FOR
SELECT 'DROP TABLE '+ NAME FROM SYSOBJECTS WHERE XTYPE='U' -- AND NAME LIKE 'xx%'
OPEN CUR_TAB
FETCH CUR_TAB INTO @SQL
WHILE @@FETCH_STATUS =0
BEGIN
EXEC(@SQL)
FETCH CUR_TAB INTO @SQL
END
CLOSE CUR_TAB
DEALLOCATE CUR_TAB
commit tran
--使用sys.foreign_keys
--disable and enable 外键
这里解释下 { CHECK | NOCHECK } CONSTRAINT
Specifies that constraint_name is enabled or disabled. This option can only be used with FOREIGN KEY and CHECK constraints. When NOCHECK is specified, the constraint is disabled and future inserts or updates to the column are not validated against the constraint conditions. DEFAULT, PRIMARY KEY, and UNIQUE constraints cannot be disabled.
上面的解释是,NOCHECK对应于disable, CHECK则对应于enable
select
'ALTER TABLE '+o.name+' NOCHECK CONSTRAINT '+fk.name+';' AS Command
from
sys.foreign_keys fk
JOIN sys.all_objects o ON (fk.parent_object_id=o.object_id)
具体会有多少条记录,取决于你的数据库里面,有多少个外键了。
然后复制结果, 粘贴出来执行. 就停用 外键约束了.
再删除数据
数据删除好了, 再启用外键约束
select
'ALTER TABLE ' + o.name + ' CHECK CONSTRAINT ' + fk.name + ';' AS Command
from
sys.foreign_keys fk
JOIN sys.all_objects o ON (fk.parent_object_id = o.object_id)
和前面的一样, 把查询出来的结果, 复制一下, 然后粘贴出来去执行.
--使用sys.foreign_key_columns
--查出外键
select name
from sys.foreign_key_columns f join sys.objects o on f.constraint_object_id=o.object_id
where f.parent_object_id=object_id('表名')
--删除外键
alter table 表名 drop constraint 外简名
--删除外键
set xact_abort on
begin tran
DECLARE @SQL VARCHAR(99)
DECLARE CUR_FK CURSOR LOCAL FOR
SELECT 'alter table ['+ OBJECT_NAME(FKEYID) + '] drop constraint ' + OBJECT_NAME(CONSTID) FROM SYSREFERENCES
--删除所有外键
OPEN CUR_FK
FETCH CUR_FK INTO @SQL
WHILE @@FETCH_STATUS =0
BEGIN
EXEC(@SQL)
FETCH CUR_FK INTO @SQL
END
CLOSE CUR_FK
DEALLOCATE CUR_FK
-- 删除所有表
DECLARE CUR_TAB CURSOR LOCAL FOR
SELECT 'DROP TABLE '+ NAME FROM SYSOBJECTS WHERE XTYPE='U' -- AND NAME LIKE 'xx%'
OPEN CUR_TAB
FETCH CUR_TAB INTO @SQL
WHILE @@FETCH_STATUS =0
BEGIN
EXEC(@SQL)
FETCH CUR_TAB INTO @SQL
END
CLOSE CUR_TAB
DEALLOCATE CUR_TAB
commit tran
--使用sys.foreign_keys
--disable and enable 外键
这里解释下 { CHECK | NOCHECK } CONSTRAINT
Specifies that constraint_name is enabled or disabled. This option can only be used with FOREIGN KEY and CHECK constraints. When NOCHECK is specified, the constraint is disabled and future inserts or updates to the column are not validated against the constraint conditions. DEFAULT, PRIMARY KEY, and UNIQUE constraints cannot be disabled.
上面的解释是,NOCHECK对应于disable, CHECK则对应于enable
select
'ALTER TABLE '+o.name+' NOCHECK CONSTRAINT '+fk.name+';' AS Command
from
sys.foreign_keys fk
JOIN sys.all_objects o ON (fk.parent_object_id=o.object_id)
具体会有多少条记录,取决于你的数据库里面,有多少个外键了。
然后复制结果, 粘贴出来执行. 就停用 外键约束了.
再删除数据
数据删除好了, 再启用外键约束
select
'ALTER TABLE ' + o.name + ' CHECK CONSTRAINT ' + fk.name + ';' AS Command
from
sys.foreign_keys fk
JOIN sys.all_objects o ON (fk.parent_object_id = o.object_id)
和前面的一样, 把查询出来的结果, 复制一下, 然后粘贴出来去执行.
--使用sys.foreign_key_columns
--查出外键
select name
from sys.foreign_key_columns f join sys.objects o on f.constraint_object_id=o.object_id
where f.parent_object_id=object_id('表名')
--删除外键
alter table 表名 drop constraint 外简名
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |