Loading ...

SQL Server: Delete constraints without knowing its name in a column | CodeAsp.Net

SQL Server: Delete constraints without knowing its name in a column

 /5
0 (0votes)

Below is the script to do the same:

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

SET @table_name = N'YourTableName'
SET @col_name = N'YourColumnName'

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

In the above script you just need to modify the following two lines and put the name of your table and column.

SET @table_name = N'YourTableName'
SET @col_name = N'YourColumnName'

Comments (no comments yet)

Top Posts