Created
August 20, 2021 15:29
-
-
Save thiagosantos/1fb45f58bb3af397232a3bdd35753497 to your computer and use it in GitHub Desktop.
Drop default constraint without knowing its name - SQL SERVER
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Answer from Philip Kelley at https://stackoverflow.com/a/1433384/4537233 | |
**/ | |
declare @schema_name nvarchar(256) | |
declare @table_name nvarchar(256) | |
declare @col_name nvarchar(256) | |
declare @Command nvarchar(1000) | |
set @schema_name = N'MySchema' | |
set @table_name = N'Department' | |
set @col_name = N'ModifiedDate' | |
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 t.schema_id = schema_id(@schema_name) | |
and c.name = @col_name | |
--print @Command | |
execute (@Command) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment