Created
August 17, 2017 11:42
-
-
Save GER-NaN/e5d32d3a6bbcb6b6eff52860248e39af to your computer and use it in GitHub Desktop.
Query for tables that have an Identity column and get the current value of the the identity column
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
| --Get tables and their current identity | |
| --This is useful to double check ID values when a database has been seeded with initial data and we want to jump the id's up by a few thousand for appearance sake | |
| --For when we dont want people to see ID = 1, Id = 2, Id =3 | |
| SELECT | |
| [schema] = s.name, | |
| [table] = t.name, | |
| IdentityValue = (IDENT_CURRENT(t.name)) | |
| FROM | |
| sys.schemas AS s INNER JOIN | |
| sys.tables AS t | |
| ON s.[schema_id] = t.[schema_id] | |
| WHERE EXISTS | |
| ( | |
| SELECT 1 FROM sys.identity_columns WHERE [object_id] = t.[object_id] | |
| ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment