2010-02-08 17 views
4

什麼是確定是否存在對列及其名稱的默認約束以及爲了在刪除之前刪除它們的任何索引的名稱列?如何刪除具有未命名默認值約束和未知索引的列

+1

你的問題是什麼? – 2010-02-08 15:46:20

+0

如何使用未命名的默認約束和索引刪除列?對不起,如果「如何」不在主題行中,但這個網站上有關於這些主題的信息很多,常見問題解答表示你可以回答你自己的問題,我想我會發佈一個簡明的解決方案,以便它可以幫助別人。 – avenmore 2010-02-08 16:21:29

+0

未來,您可以按照以下流程進行操作: - >寫問題 - >發佈問題 - >寫答案(在答題部分) - >發帖答案 - >標記爲正確答案。 雖然如果你喜歡博客,這聽起來像是一件很棒的話題。 – 2010-02-08 20:05:10

回答

9

以下實用程序將執行此任務。

if (exists (select * from [dbo].sysobjects where (id = object_id(N'[dbo]._spDropDefaultValueConstraint')) and (xtype = 'P'))) 
drop procedure [dbo]._spDropDefaultValueConstraint 
GO 

create procedure [dbo]._spDropDefaultValueConstraint 
@TableName varchar(256), 
@ColumnName varchar(256) 
as 
/* This proc will drop the default value constraint on 
a column even when you don't know what its name is. 
*/ 
declare @ConstraintName varchar(256) 
set @ConstraintName = (
select 
    dobj.name 
from sys.columns col 
    left outer join sys.objects dobj 
    on dobj.object_id = col.default_object_id and dobj.type = 'D' 
where col.object_id = object_id('[dbo].'[email protected]) 
and dobj.name is not null 
and col.name = @ColumnName) 

if(isnull(@ConstraintName, '') <> '') 
exec('alter table [dbo].['[email protected]+'] drop constraint ['[email protected]+']') 

GO 

------------------------------------------------------------------------------------------- 

if (exists (select * from [dbo].sysobjects where (id = object_id(N'[dbo]._spDropIndexesForColumn')) and (xtype = 'P'))) 
drop procedure [dbo]._spDropIndexesForColumn 
GO 

create procedure [dbo]._spDropIndexesForColumn 
@TableName varchar(256), 
@ColumnName varchar(256) 
as 
/* This proc will drop all indexes on a column, both indexes 
and unique constraints as well as multi-part indexes that reference it. 
*/ 
declare @IndexName varchar(256) 
declare @IsPrimaryKey bit 
declare @IsUniqueConstraint bit 

declare crIndexes cursor for 
select 
    ind.name, ind.is_primary_key, ind.is_unique_constraint 
from 
    sys.indexes ind 
    inner join sys.index_columns ic on ind.object_id = ic.object_id and ind.index_id = ic.index_id 
    inner join sys.columns col on ic.object_id = col.object_id and ic.column_id = col.column_id 
    inner join sys.tables t on ind.object_id = t.object_id 
where 
    t.name = @TableName and 
    col.name = @ColumnName  
open crIndexes 
fetch next from crIndexes into @IndexName, @IsPrimaryKey, @IsUniqueConstraint 
while(@@fetch_status = 0) begin 
if(@IsPrimaryKey = 1) or (@IsUniqueConstraint = 1) 
    exec('alter table [dbo].['[email protected]+'] drop constraint ['[email protected]+']') 
else 
    exec('drop index [dbo].['[email protected]+'].['[email protected]+']') 
fetch next from crIndexes into @IndexName, @IsPrimaryKey, @IsUniqueConstraint 
end 
close crIndexes 
deallocate crIndexes 

GO 

------------------------------------------------------------------------------------------- 

if (exists (select * from [dbo].sysobjects where (id = object_id(N'[dbo]._spDropColumn')) and (xtype = 'P'))) 
drop procedure [dbo]._spDropColumn 
GO 

create procedure [dbo]._spDropColumn 
@TableName varchar(256), 
@ColumnName varchar(256) 
as 
/* This proc will drop a column (first dropping the default value 
constraint and any indexes if they exist) if it exists. 
*/ 
if (exists (select * from [dbo].sysobjects where (id = object_id('[dbo].'[email protected])) and (xtype = 'U'))) and 
    (exists (select * from [dbo].syscolumns where (id = object_id('[dbo].'[email protected])) and (name = @ColumnName))) begin 
exec [dbo]._spDropIndexesForColumn @TableName, @ColumnName 
exec [dbo]._spDropDefaultValueConstraint @TableName, @ColumnName 
exec('alter table [dbo].['[email protected]+'] drop column ['[email protected]+']') 
end 
GO 

它是那麼容易地調用如下:

exec [dbo]._spDropColumn 'TableName', 'ColumnName' 

我沒有看過外鍵約束,因爲我們不使用它們,但也許他們可以列入了。