Answers for "drop column with default value in sql"

SQL
0

sql server drop constraint default value

ALTER TABLE [TheTable] DROP CONSTRAINT [TheDefaultConstraint]
Posted by: Guest on June-24-2020
0

sql script to remove default from column

DECLARE @tableName VARCHAR(MAX) = '<MYTABLENAME>'
DECLARE @columnName VARCHAR(MAX) = '<MYCOLUMNAME>'
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 = @columnName AND object_id = OBJECT_ID(@tableName))
IF @ConstraintName IS NOT NULL
    EXEC('ALTER TABLE '+@tableName+' DROP CONSTRAINT ' + @ConstraintName)
    
-- Just replace <MYTABLENAME> and <MYCOLUMNNAME>
Posted by: Guest on March-31-2021

Code answers related to "drop column with default value in sql"

Code answers related to "SQL"

Browse Popular Code Answers by Language