Skip to content

Instantly share code, notes, and snippets.

@richardbasile
Created March 16, 2018 13:03
Show Gist options
  • Save richardbasile/d8ec789ba3e746a5c0b7be8dd7cbe738 to your computer and use it in GitHub Desktop.
Save richardbasile/d8ec789ba3e746a5c0b7be8dd7cbe738 to your computer and use it in GitHub Desktop.
SQL Server - Rename Foreign Keys
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