返回顶部
首页 > 资讯 > 数据库 >记录sql server 的批量删除主外键的sql语句
  • 683
分享到

记录sql server 的批量删除主外键的sql语句

摘要

select b.name TableName,a.name TypeName,a.* from sysobjects a inner join sysobjects b on a.parent_obj=b.id and b.x


	记录sql server 的批量删除主外键的sql语句
[数据库教程]

select b.name TableName,a.name TypeName,a.* from sysobjects a inner join sysobjects b on a.parent_obj=b.id and b.xtype=U


--删除约束  F外键、PK主键、D 约束、UQ 唯一约束
declare @tableName varchar(max),@typeName varchar(max)
declare fk_cursor cursor for select b.name TableName,a.name TypeName from sysobjects a inner join sysobjects b on a.parent_obj=b.id and b.xtype=U where a.xtype=PK

open fk_cursor

fetch next from fk_cursor into @tableName,@typeName
while @@FETCH_STATUS = 0
begin 
    exec (ALTER TABLE [dbo].[[email protected]+] DROP CONSTRAINT [[email protected]+])
    fetch next from fk_cursor into @tableName,@typeName
end
close fk_cursor
deallocate fk_cursor
select b.name TableName,a.name IndexName from sys.sysindexes a right join sys.sysobjects b on a.id=b.id where b.xtype=U and a.name is not null

--删除IX 索引
declare @tableName varchar(max),@indexName varchar(max)
declare index_cursor cursor for select b.name TableName,a.name IndexName from sys.sysindexes a right join sys.sysobjects b on a.id=b.id where b.xtype=U and a.name is not null

open index_cursor

fetch next from index_cursor into @tableName,@indexName
while @@FETCH_STATUS = 0
begin 
    exec (DROP INDEX [[email protected]+] ON [dbo].[[email protected]+])
    fetch next from index_cursor into @tableName,@indexName
end
close index_cursor
deallocate index_cursor



select b.name TableName,a.name ColumnName,a.* from syscolumns a 
inner join sysobjects b on b.id=a.id and b.xtype=U
inner join systypes c on a.xtype=c.xtype and c.name=uniqueidentifier

--修改uniqueidentifier的类型为nvarchar(max)
declare @tableName varchar(max),@columnName varchar(max)
declare change_type_cursor cursor for select b.name TableName,a.name ColumnName from syscolumns a 
inner join sysobjects b on b.id=a.id and b.xtype=U
inner join systypes c on a.xtype=c.xtype and c.name=uniqueidentifier

open change_type_cursor
fetch next from change_type_cursor into @tableName,@columnName
while @@FETCH_STATUS =0
begin
    exec (ALTER TABLE [dbo].[[email protected]+] ALTER COLUMN [[email protected]+] nvarchar(max) NOT NULL)
    fetch next from change_type_cursor into @tableName,@columnName
end
close change_type_cursor
deallocate change_type_cursor

 

记录SQL Server 的批量删除主外键的sql语句

原文地址:https://www.cnblogs.com/dazen/p/13307105.html

您可能感兴趣的文档:

--结束END--

本文标题: 记录sql server 的批量删除主外键的sql语句

本文链接: https://lsjlt.com/news/7217.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作