sql script to remove default from column

Solutions on MaxInterview for sql script to remove default from column by the best coders in the world

showing results for - "sql script to remove default from column"
Mariana
10 Mar 2018
1DECLARE @tableName VARCHAR(MAX) = '<MYTABLENAME>'
2DECLARE @columnName VARCHAR(MAX) = '<MYCOLUMNAME>'
3DECLARE @ConstraintName nvarchar(200)
4SELECT @ConstraintName = Name 
5FROM SYS.DEFAULT_CONSTRAINTS
6WHERE PARENT_OBJECT_ID = OBJECT_ID(@tableName) 
7AND PARENT_COLUMN_ID = (
8    SELECT column_id FROM sys.columns
9    WHERE NAME = @columnName AND object_id = OBJECT_ID(@tableName))
10IF @ConstraintName IS NOT NULL
11    EXEC('ALTER TABLE '+@tableName+' DROP CONSTRAINT ' + @ConstraintName)
12    
13-- Just replace <MYTABLENAME> and <MYCOLUMNNAME>