Created
March 16, 2018 13:03
-
-
Save richardbasile/d8ec789ba3e746a5c0b7be8dd7cbe738 to your computer and use it in GitHub Desktop.
SQL Server - Rename Foreign Keys
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
select 'exec sp_rename N''dbo.[' + fk.name + ']'', N''FK_' + upper(t.name) + '_' + lower(c.name) + ''', @objtype = ''OBJECT'';' | |
from sys.foreign_keys fk | |
join sys.foreign_key_columns fkc on fkc.constraint_object_id = fk.object_id | |
join sys.columns c on c.object_id = fk.parent_object_id | |
and c.column_id = fkc.parent_column_id | |
join sys.tables t on t.object_id = fk.parent_object_id | |
where fk.name <> 'FK_' + upper(t.name) + '_' + lower(c.name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment