Using the following SP, you can dynamically find default constraint of a column and drop that.
CREATE PROC dbo.DropDefaultConstraints
(
@schema_name NVARCHAR(250),
@table_name NVARCHAR(256),
@col_name NVARCHAR(256)
)
AS
BEGIN
DECLARE @Command NVARCHAR(1000)
SELECT @Command = 'ALTER TABLE ' + @schema_name +'.'+ @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)
END
GO
Result : ALTER TABLE dbo.Person DROP CONSTRAINT DF_Person_Nationality
ref: stackoverflow.com
(
@schema_name NVARCHAR(250),
@table_name NVARCHAR(256),
@col_name NVARCHAR(256)
)
AS
BEGIN
DECLARE @Command NVARCHAR(1000)
SELECT @Command = 'ALTER TABLE ' + @schema_name +'.'+ @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)
END
GO
Result : ALTER TABLE dbo.Person DROP CONSTRAINT DF_Person_Nationality
ref: stackoverflow.com
0 comments:
Post a Comment