Skip to content

Instantly share code, notes, and snippets.

@GER-NaN
Created August 17, 2017 11:42
Show Gist options
  • Select an option

  • Save GER-NaN/e5d32d3a6bbcb6b6eff52860248e39af to your computer and use it in GitHub Desktop.

Select an option

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
--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