使用数据定义语言删除列和所有依赖对象

发布于 2024-10-08 15:11:00 字数 350 浏览 4 评论 0原文

我需要从表中删除一列,但是当我尝试删除它时:

对象“object_name”是依赖的 在“column_name”列上。

ALTER TABLE DROP COLUMN column_name 失败,因为 一个或多个对象访问 this 专栏。

我可以在系统表中查找依赖项并手动删除它,但我需要进行迁移(使用 SQL DDL),因此团队的所有其他成员只需执行更新、运行迁移,而不必弄乱系统对象。

I need to remove a column from a table, but when I try to remove it:

The object 'object_name' is dependent
on column 'column_name'.

ALTER TABLE DROP COLUMN column_name failed because
one or more objects access this
column.

I can look for the dependency in the system tables and remove it manually, but I need to do a migration (using SQL DDL) so all others members of the team just do the update, run the migration and don´t have to mess up up system objects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝戈者 2024-10-15 15:11:00

尝试此代码:

Declare @TABLENAME varchar(max), @COLUMN varchar(max)
SET @TABLENAME = 'YOURTableName'
SET @COLUMN = 'YOURColumnName'
Declare @CONSTRAINT varchar(max)
                    set @CONSTRAINT ='ALTER TABLE '+@TABLENAME+' DROP CONSTRAINT '
                    set @CONSTRAINT = @CONSTRAINT + (select SYS_OBJ.name as CONSTRAINT_NAME
                    from sysobjects SYS_OBJ
                    join syscomments SYS_COM on SYS_OBJ.id = SYS_COM.id
                    join sysobjects SYS_OBJx on SYS_OBJ.parent_obj = SYS_OBJx.id 
                    join sysconstraints SYS_CON on SYS_OBJ.id = SYS_CON.constid
                    join syscolumns SYS_COL on SYS_OBJx.id = SYS_COL.id
                    and SYS_CON.colid = SYS_COL.colid
                    where
                    SYS_OBJ.uid = user_id() and SYS_OBJ.xtype = 'D'
                    and SYS_OBJx.name=@TABLENAME and SYS_COL.name=@COLUMN)
                    exec(@CONSTRAINT)

然后运行常规更改表:

ALTER TABLE YOURTABLENAME
DROP COLUMN YOURCOLUMNNAME

使用第一个代码,您可以删除对该列的所有依赖项,然后您可以毫无问题地删除它。

编辑 - 删除默认值约束:

上面的代码似乎没有删除 DEFAULT_CONSTRAINTS 因此,在这种情况下您还必须使用:

DECLARE @ConstraintName nvarchar(200)
SELECT @ConstraintName = Name FROM SYS.DEFAULT_CONSTRAINTS WHERE PARENT_OBJECT_ID = OBJECT_ID('__TableName__') AND PARENT_COLUMN_ID = (SELECT column_id FROM sys.columns WHERE NAME = N'__ColumnName__' AND object_id = OBJECT_ID(N'__TableName__'))
IF @ConstraintName IS NOT NULL
    EXEC('ALTER TABLE __TableName__ DROP CONSTRAINT ' + @ConstraintName)

Try this code:

Declare @TABLENAME varchar(max), @COLUMN varchar(max)
SET @TABLENAME = 'YOURTableName'
SET @COLUMN = 'YOURColumnName'
Declare @CONSTRAINT varchar(max)
                    set @CONSTRAINT ='ALTER TABLE '+@TABLENAME+' DROP CONSTRAINT '
                    set @CONSTRAINT = @CONSTRAINT + (select SYS_OBJ.name as CONSTRAINT_NAME
                    from sysobjects SYS_OBJ
                    join syscomments SYS_COM on SYS_OBJ.id = SYS_COM.id
                    join sysobjects SYS_OBJx on SYS_OBJ.parent_obj = SYS_OBJx.id 
                    join sysconstraints SYS_CON on SYS_OBJ.id = SYS_CON.constid
                    join syscolumns SYS_COL on SYS_OBJx.id = SYS_COL.id
                    and SYS_CON.colid = SYS_COL.colid
                    where
                    SYS_OBJ.uid = user_id() and SYS_OBJ.xtype = 'D'
                    and SYS_OBJx.name=@TABLENAME and SYS_COL.name=@COLUMN)
                    exec(@CONSTRAINT)

and then run your regular alter table:

ALTER TABLE YOURTABLENAME
DROP COLUMN YOURCOLUMNNAME

With the first code you remove all the dependencies on that column and then you can remove it without problems.

EDIT - Removing Default Values Constraints:

The code above does not seems to remove DEFAULT_CONSTRAINTS so, in that case you must also use:

DECLARE @ConstraintName nvarchar(200)
SELECT @ConstraintName = Name FROM SYS.DEFAULT_CONSTRAINTS WHERE PARENT_OBJECT_ID = OBJECT_ID('__TableName__') AND PARENT_COLUMN_ID = (SELECT column_id FROM sys.columns WHERE NAME = N'__ColumnName__' AND object_id = OBJECT_ID(N'__TableName__'))
IF @ConstraintName IS NOT NULL
    EXEC('ALTER TABLE __TableName__ DROP CONSTRAINT ' + @ConstraintName)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文