Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop column and all dependent objects using data definition language

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.

like image 386
aGuy Avatar asked Dec 16 '10 16:12

aGuy


People also ask

How do I drop a dependent column in SQL?

Drop Column – SSMS You can also delete a column using the SQL Server Management Studio. Start by selecting your target database -> Expand to tables -> Expand target table and select columns. Expand the columns menu and select your target column. Right-click and select the delete option.

Can I drop column with data?

Obviously, if you have data and you drop a column, you will lose the data in that column. If you don't want to lose that data, don't drop it. Dropping a columns does not delete rows.

What is the syntax of dropping a column?

Syntax. DROP COLUMN column_name; In the syntax mentioned above, First, you define the table name from which you wish to remove or delete the column.

How do I drop a column in sqlite?

You can not use the ALTER TABLE statement to drop a column in a table. Instead you will need to rename the table, create a new table, and copy the data into the new table.


1 Answers

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)
like image 160
JOBG Avatar answered Oct 09 '22 02:10

JOBG