So, here's what I've come up with...
SET NOCOUNT ON;
DECLARE @MisnamedConstraints TABLE
(
OldConstraintName SYSNAME,
NewConstraintName SYSNAME,
ParentObject SYSNAME,
ColumnName SYSNAME,
ConstraintDefinition NVARCHAR(Max)
)
-- Find all constraints on multistatement table-valued functions where the
-- constraint name doesn't match the standard.
INSERT INTO @MisnamedConstraints
SELECT
DC.[name] 'Constraint Object',
'DF_' + O.[name] + '_' + C.[name] 'Standards Compliant Constraint Name',
O.[name] 'Parent Object',
C.[name] 'Column Name',
DC.[definition] 'Constraint Defintion'
FROM [sys].[default_constraints] DC
INNER JOIN [sys].[objects] O
ON DC.[parent_object_id] = O.[object_id]
INNER JOIN [sys].[columns] C
ONC.[object_id] = DC.[parent_object_id]
AND C.[column_id] = DC.[parent_column_id]
WHERE O.[type] = 'TF'
AND DC.[name] <> 'DF_' + O.[name] + '_' + C.name;
-- If there are naming collisions, remove them and output them for further research.
-- It is possible that a naming collision will be cleared by this script, and that
-- running this script a second time will not result in a collision.
DELETE MC
OUTPUT
DELETED.OldConstraintName,
DELETED.NewConstraintName,
DELETED.ParentObject,
DELETED.ColumnName,
DELETED.ConstraintDefinition
FROM [sys].[default_constraints] DC
INNER JOIN @MisnamedConstraints MC
ON DC.[name] = MC.[NewConstraintName];
-- Loop through all the non-colliding constraints and rename them to
-- the standards compliant name.
DECLARE @OldConstraintName SYSNAME;
DECLARE @NewConstraintName SYSNAME;
DECLARE @OBJECT_TYPE VARCHAR(13);SET @OBJECT_TYPE = 'OBJECT';
SELECT
@OldConstraintName = OldConstraintName,
@NewConstraintName = NewConstraintName
FROM @MisnamedConstraints;
WHILE @@RowCount > 0
BEGIN
EXECUTE sp_rename @OldConstraintName, @NewConstraintName, @OBJECT_TYPE;
DELETE
FROM @MisnamedConstraints
WHERE OldConstraintName = @OldConstraintName;
SELECT
@OldConstraintName = OldConstraintName,
@NewConstraintName = NewConstraintName
FROM @MisnamedConstraints;
END
I've done some preliminary testing and things appear to function fine after the rename.
Thanks,
MKE Data Guy