Last active
January 6, 2021 14:56
-
-
Save gte445e/200fd224bfb6db3f6a6b73a5a0287fdc to your computer and use it in GitHub Desktop.
Sql Primary Key Checks
This file contains hidden or 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 @objtype = ''INDEX'', @objname = ''' + quotename(s.name) + '.' + quotename(t.name) + '.' + quotename(pk.name) + ''', @newname = ''' + 'PK_' + t.name + '''' as RenameScript | |
from | |
sys.schemas s | |
inner join | |
sys.tables t | |
on s.schema_id = t.schema_id | |
inner join sys.key_constraints pk | |
on t.object_id = pk.parent_object_id | |
where | |
pk.type = 'PK' | |
and pk.name <> 'PK_' + t.name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment