- [sql]
-
------------------------------------------------------------ mp_DropColConstraint-- 功能:删除某个表的某列的所有约束-- 入口: www.2cto.com-- @TableName NVARCHAR(128) -- 表名-- @ColumnName NVARCHAR(128) -- 列名----------------------------------------------------------if OBJECT_ID(N'dbo.mp_DropColConstraint', N'P') is not nulldrop procedure dbo.mp_DropColConstraintgocreate procedure dbo.mp_DropColConstraint@TableName NVARCHAR(128),@ColumnName NVARCHAR(128)asbeginif OBJECT_ID(N'#t', N'TB') is not nulldrop table #t-- 查询主键约束、非空约束等select ROW_NUMBER() over(order by CONSTRAINT_NAME) id, CONSTRAINT_NAME into #t from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE where TABLE_CATALOG=DB_NAME()and TABLE_NAME=@TableName and COLUMN_NAME=@ColumnName-- 查询默认值约束declare @cdefault int, @cname varchar(128)select @cdefault=cdefault from sys.syscolumns where name=@ColumnName and id=OBJECT_ID(@TableName) www.2cto.comselect @cname=name from sys.sysobjects where id=@cdefaultif @cname is not nullinsert into #t select coalesce(max(id), 0)+1, @cname from #tdeclare @i int, @imax intselect @i=1, @imax=max(id) from #twhile @i <= @imaxbeginselect @cname=CONSTRAINT_NAME from #t where id=@iexec('alter table ' + @tablename + ' drop constraint ' + @cname)set @i = @i + 1enddrop table #tendgo------------------------------------------- mfn_IsColumnExists-- 功能:判断字段是否存在-- 入口:-- @TableName NVARCHAR(128) -- 表名-- @ColumnName NVARCHAR(128) -- 列名-- 出口:-- BIT 1=存在,0=不存在----------------------------------------if OBJECT_ID(N'dbo.mfn_IsColumnExists', N'FN') is not nulldrop function dbo.mfn_IsColumnExistsgocreate function dbo.mfn_IsColumnExists(@TableName NVARCHAR(128), @ColumnName NVARCHAR(128)) www.2cto.comreturns bitasbegindeclare @rt bitset @rt=0if (select name from sys.syscolumns where name=@ColumnName and id=OBJECT_ID(@TableName)) is not nullset @rt=1return @rtendgo---------------------------------------------------- mfn_GetColumnIndexes-- 功能:查询某个字段的所有索引-- 入口:-- @TableName NVARCHAR(128) -- 表名-- @ColumnName NVARCHAR(128) -- 列名(字段名)-- 出口:返回一个结果集:-- id int -- 序号,从1开始-- name nvarchar(128) -- 索引名称--------------------------------------------------if OBJECT_ID(N'dbo.mfn_GetColumnIndexes', N'TF') is not nulldrop function dbo.mfn_GetColumnIndexesgocreate function dbo.mfn_GetColumnIndexes(@TableName NVARCHAR(128), @ColumnName NVARCHAR(128))returns @ret table(id int,name NVARCHAR(128)) www.2cto.comasbegindeclare @tid int, @colid int-- 先查询出表id和列idselect @tid=OBJECT_ID(@tablename)select @colid=colid from sys.syscolumns where id=@tid and name=@columnname-- 查询出索引名称insert into @ret select ROW_NUMBER() OVER(ORDER BY cols.index_id) as id, inds.name idxname from sys.index_columns colsleft join sys.indexes inds on cols.object_id=inds.object_id and cols.index_id=inds.index_idwhere cols.object_id=@tid and column_id=@colidreturnendgo------------------------------------------------------ mp_DropColumnIndexes-- 功能:删除指定列的所有索引-- 入口:-- @TableName NVARCHAR(128) 表名-- @ColumnName NVARCHAR(128) 列名--------------------------------------------------if OBJECT_ID(N'dbo.mp_DropColumnIndexes', N'P') is not nulldrop procedure dbo.mp_DropColumnIndexesgocreate procedure dbo.mp_DropColumnIndexes@TableName NVARCHAR(128),@ColumnName NVARCHAR(128)as www.2cto.combeginif OBJECT_ID(N'#t', N'TB') is not nulldrop table #tcreate table #t(id int,name nvarchar(128))insert into #t select * from mfn_GetColumnIndexes(@TableName, @ColumnName)-- 删除索引declare @i int, @imax int, @idxname nvarchar(128)select @i=1, @imax=COALESCE(max(id), 0) from #twhile @i<=@imaxbeginselect @idxname=name from #tEXEC('drop index ' + @idxname + ' on ' + @tablename)set @i=@i+1enddrop table #tendgo-------------------------------------------------- mp_DropColConstraintAndIndex-- 功能:删除指定字段的所有约束和索引-- 入口:-- @TableName NVARCHAR(128) -- 表名-- @ColumnName NVARCHAR(128) -- 列名------------------------------------------------if OBJECT_ID(N'dbo.mp_DropColConstraintAndIndex', N'P') is not nulldrop procedure dbo.mp_DropColConstraintAndIndexgo www.2cto.comcreate procedure dbo.mp_DropColConstraintAndIndex@TableName NVARCHAR(128),@ColumnName NVARCHAR(128)asbeginexec dbo.mp_DropColConstraint @TableName, @ColumnNameexec dbo.mp_DropColumnIndexes @TableName, @ColumnNameendgo作者 niniu
MSSQL如何删除字段的所有约束和索引
最新推荐文章于 2022-12-11 15:35:39 发布