SQL Server 数据库技术之:Drop All Constraints
作者:本站原创转载自:xin3721网络学院更新时间:2010/10/19

 Drop All Constraints
Drop All Constraints


sp_drop_constraints will drop all constraints on the specified table, including CHECK, FOREIGN KEY, PRIMARY KEY, UNIQUE, and DEFAULT constraints. Compile it in your master database and use it from any user database. 

Author: Doug Bass 

SET QUOTED_IDENTIFIER  OFF    SET ANSI_NULLS  ON 
GO

if exists (select * from sysobjects where id = object_id(N'[dbo].[sp_drop_constraints]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_drop_constraints]
GO

create proc sp_drop_constraints

 @tablename sysname

as

-- name:   sp_drop_constraints
-- author: douglas bass
-- date:   06/19/2000

-- sp_drop_constraints will drop all constraints on the specified table, 
-- including CHECK, FOREIGN KEY, PRIMARY KEY, UNIQUE, and DEFAULT constraints.  
-- Compile it in your master database and use it from any user database.

set nocount on

declare @constname sysname,
 @cmd  varchar(1024)

declare curs_constraints cursor for
 select  name
 from  sysobjects 
 where  xtype in ('C', 'F', 'PK', 'UQ', 'D')
 and (status & 64) = 0
 and     parent_obj = object_id(@tablename)

open curs_constraints

fetch next from curs_constraints into @constname
while (@@fetch_status = 0)
begin
 select @cmd = 'ALTER TABLE ' + @tablename + ' DROP CONSTRAINT ' + @constname
 exec(@cmd)
 fetch next from curs_constraints into @constname
end

close curs_constraints
deallocate curs_constraints

return 0


GO
SET QUOTED_IDENTIFIER  OFF    SET ANSI_NULLS  ON 
GO

关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们     黑ICP备07002182号