1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
DECLARE @TableSchema NVARCHAR(255) = ''; DECLARE @TableName NVARCHAR(255) = ''; DECLARE @ColumnName NVARCHAR(255) = ''; DECLARE @sql NVARCHAR(MAX) WHILE 1=1 BEGIN SELECT TOP 1 @sql = N'ALTER TABLE ' + @TableSchema + '.' + @TableName + ' DROP CONSTRAINT ['+dc.NAME+N']' FROM sys.default_constraints dc LEFT JOIN sys.columns c ON c.default_object_id = dc.object_id WHERE dc.parent_object_id = OBJECT_ID(@TableSchema + '.' + @TableName) AND c.name = @ColumnName IF @@ROWCOUNT = 0 BREAK EXEC (@sql) END IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA=@TableSchema AND TABLE_NAME = @TableName AND COLUMN_NAME = @ColumnName ) BEGIN EXEC ('ALTER TABLE ' + @TableSchema + '.' + @TableName + ' DROP COLUMN ' + @ColumnName) END GO |
Login