4
열과 이름에 기본 제약 조건이 있는지, 그리고 삭제하기 전에 삭제할 인덱스의 이름을 결정하는 방법은 무엇입니까? 칼럼?unamed 기본 값 제약 조건 및 알 수없는 인덱스가있는 열을 삭제하는 방법
열과 이름에 기본 제약 조건이 있는지, 그리고 삭제하기 전에 삭제할 인덱스의 이름을 결정하는 방법은 무엇입니까? 칼럼?unamed 기본 값 제약 조건 및 알 수없는 인덱스가있는 열을 삭제하는 방법
다음 유틸리티 procs가 작업을 수행합니다. 우리가 사용하지 않는 내가 외래 키 제약 조건에보고하지 않은
exec [dbo]._spDropColumn 'TableName', 'ColumnName'
, 그러나 아마 그들도 포함될 수 :
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
은 다음과 같이 호출 다음 쉽다.
무엇이 당신의 질문입니까? –
이름없는 기본 제약 조건 및 인덱스가있는 열을 삭제하는 방법은 무엇입니까? 미안 "방법"이 제목에 없다면이 주제와 관련하여이 사이트에 정보가 너무 많아서 자주 묻는 질문에 대답 할 수 있다고 대답합니다. 간결한 솔루션으로 다른 사람들을 도울 수 있습니다. – avenmore
앞으로이 프로세스를 수행 할 수 있습니다 : -> 질문 쓰기 -> 질문 게시 -> 답변 쓰기 (대답 섹션에서) -> 답변 게시 -> 정답으로 표시하십시오. 블로그를 좋아한다면 멋진 주제가 될 것 같은데요. –