Removing a column from Sql having a default value constraint

If you have created a table in Microsoft SQL and along with a table you have a column with a default value, then the name of the constraint will be generate by the SQL framework unless given. This will be will have some random GUID which will keep it hard to know when you want to drop a column. There could be certain sitations where you want to directly delete the column and there are situations where the column should be dropped without having the knowldge of constaint name.

You can acheive that by using the following sql

 

DECLARE @ConstraintName nvarchar(200) 
BEGIN 
    SELECT @ConstraintName = Name 
    FROM SYS.DEFAULT_CONSTRAINTS 
    WHERE PARENT_OBJECT_ID = OBJECT_ID('TABLE____NAME') 
    AND PARENT_COLUMN_ID = 
        (SELECT column_id 
         FROM sys.columns 
         WHERE NAME = N'COLUMN_____NAME' 
         AND object_id = OBJECT_ID(N'TABLE____NAME')
        ) if(@ConstraintName IS NOT NULL) 

    BEGIN EXEC(
        'ALTER TABLE TABLE____NAME DROP CONSTRAINT ' + @ConstraintName
        )
   ALTER TABLE TABLE____NAME 
   DROP COLUMN COLUMN_____NAME 
END