Last active
November 14, 2018 17:05
-
-
Save aaronhoffman/f6067b3e7787a34dd44db7a8f3969314 to your computer and use it in GitHub Desktop.
rename sql constraints to match convension
This file contains 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
-- script to generate sp_rename commands for all check constraints | |
select | |
ck.name as current_name | |
,po.name as ParentObject | |
,po.type_desc as ParentType | |
,ISNULL(pc.name, N'') as ColumnName | |
,'exec sp_rename @objname = N''[' + ps.name + '].[' + ck.name + ']'', @newname = ''CK_' + po.name + '_' + pc.name + '''' as rename_cmd | |
from | |
sys.check_constraints ck | |
left join sys.objects po on (ck.parent_object_id = po.object_id) | |
left join sys.schemas ps on (po.schema_id = ps.schema_id) | |
left join sys.columns pc on (ck.parent_object_id = pc.object_id and ck.parent_column_id = pc.column_id) |
This file contains 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
-- script to generate sp_rename commands for all default constraints | |
select | |
df.name AS current_name | |
,po.name AS ParentObject | |
,po.type_desc AS ParentType | |
,isnull(pc.name, N'') AS ColumnName | |
,'exec sp_rename @objname = N''[' + ps.name + '].[' + df.name + ']'', @newname = ''DF_' + po.name + '_' + pc.name + '''' as rename_cmd | |
from | |
sys.default_constraints df | |
left join sys.objects po on (df.parent_object_id = po.object_id) | |
left join sys.schemas ps on (po.schema_id = ps.schema_id) | |
left join sys.columns pc on (df.parent_object_id = pc.object_id and df.parent_column_id = pc.column_id) |
This file contains 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
-- script to generate sp_rename commands for all foreign keys | |
select | |
co.name as current_fk_name | |
,po.name as parent_table | |
,pc.name as parent_column | |
,ro.name as ref_table | |
,rc.name as ref_column | |
,'exec sp_rename @objname = ''[' + cs.name + '].[' + co.name + ']'', @newname = ''FK_' + po.name + '_' + pc.name + '''' as rename_cmd | |
from | |
sys.foreign_key_columns fk | |
left join sys.objects co on (fk.constraint_object_id = co.object_id) | |
left join sys.schemas cs on (co.schema_id = cs.schema_id) | |
left join sys.objects po on (fk.parent_object_id = po.object_id) | |
left join sys.columns pc on (fk.parent_object_id = pc.object_id and fk.parent_column_id = pc.column_id) | |
left join sys.objects ro on (fk.referenced_object_id = ro.object_id) | |
left join sys.columns rc on (fk.referenced_object_id = rc.object_id and fk.referenced_column_id = rc.column_id) |
This file contains 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
-- script to generate sp_rename commands for each existing sys.key_constraint PK entry | |
select | |
c.name as current_pk_name | |
,po.name as table_name | |
,'exec sp_rename @objname = N''[' + ps.name + '].[' + c.name + ']'', @newname = ''PK_' + po.name + '''' as rename_cmd | |
from | |
sys.key_constraints c | |
left join sys.objects po on (c.parent_object_id = po.object_id) | |
left join sys.schemas ps on (po.schema_id = ps.schema_id) | |
where | |
c.[type] = 'PK' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment