Data Browser - Viewing Site  Sector 23 Code Bank Logged in as:  Guest  




           


Drop default constraint without knowing name
To remove a default constraint on a SQL Server column if you don't know its name, use the following.
(replace tbl and col with your table/column name)

DECLARE @table_name nvarchar(256)
DECLARE @col_name nvarchar(256)
DECLARE @Command nvarchar(1000)

SET @table_name = 'tbl'
SET @col_name = 'col'

SELECT @Command = 'ALTER TABLE ' + @table_name + ' drop constraint ' + d.name
FROM sys.tables t
JOIN sys.default_constraints d
ON d.parent_object_id = t.object_id
JOIN sys.columns c
ON c.object_id = t.object_id
AND c.column_id = d.parent_column_id
WHERE t.name = @table_name AND c.name = @col_name

-- print @Command

execute (@Command)

Created By: amos 12/18/2013 5:44:17 PM