I ended up taking a slightly different approach and created a stored procedure to call before creating a default constraint. The stored procedure is merely an IF EXISTS...DROP CONSTRAINT query, but determines the default name and only executes if there is in fact, a default constraint currently in place. Originally intended for use within SQL scripts sent to users, this method will not cause an error if ran more than once before a new default constraint is created.
[font="Courier New"]CREATE PROCEDURE [dbo].[spDropDefaultConstraint]
(
@tableName varchar(100),
@columnName varchar(100)
)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @dfName varchar(100)
SELECT @dfName = df.[name]
FROM sys.columns c
JOIN sys.default_constraints df
ON df.parent_object_id = c.object_id
AND df.parent_column_id = c.column_id
WHERE c.object_id = OBJECT_ID(@tableName)
AND c.[name] = @columnName
IF @dfName IS NOT NULL
BEGIN
EXEC ('ALTER TABLE ' + @tableName + ' DROP CONSTRAINT ' + @dfName)
PRINT 'Constraint [' + @dfName + '] dropped for ' + @tableName + '.' + @columnName
END
ELSE
PRINT 'Constraint not found for ' + @tableName + '.' + @columnName
END[/font]
So instead of the usual:
[font="Courier New"]ALTER TABLE invrecvr ADD CONSTRAINT DF_invrecr_IsExcluded DEFAULT 0 FOR IsExcluded[/font]
And receiving the warning: [font="Courier New"]Column already has a DEFAULT bound to it.[/font]
Just add this line prior to the ALTER TABLE command:
[font="Courier New"]EXEC spDropDefaultConstraint @tableName = 'invrecvr', @columnName = 'IsExcluded'[/font]
This could have easily been incorpoarated as a function, but either way, it solves the problem.
Michael