I'm hoping someone can point me in the right direction on this one. Here's my workings out so far.
SELECT * FROM sys.identity_columns
is a system view which gives "last_value" but the definition for that view uses an internal function IdentityProperty(colName, 'LastValue')
– so that's a dead end (not pulling it from a system table there).
Everywhere (I've looked) on the internet suggests using DBCC IDENT_...
commands to uncover the value but that still leaves me in the dark as to where it's actually stored.
So, I arrived at searching the individual pages with DBCC PAGE(TestDB,1,1325,3)
against my test harness db and using the RESEED
command to reseed between values 10 and 12.
In doing this, I noticed the hex values on the IAM: Header
, IAM: Single Page Allocations
and IAM: Extent Alloc Status Slot 1
all changed. (And realised they change periodically anyway along with the bUse1 value which changes incrementally by itself too).
So another dead-end and I'm all out of ideas. Where else can I search?
I'm running SQL Server 2014. I have an insatiable thirst for internals knowledge and have yet to come across anything as elusive as this. It's caught my attention because in theory, it (an absolute value) is stored somewhere and should (arguably) be locatable. In my quest to unearth locations of internally stored data / meta data, this particular value strikes me as particularly elusive. I'm guessing/hoping that someone will come along and tell me, you can get it with DBCC PAGE
but I was looking in the wrong place.
Best Answer
If you can access the DAC (Dedicated Administrator Console), you can inspect the value of the identity column, for
INT
columns, by looking at theidtval
column insys.syscolpars
.Thanks to Martin Smith for directing me to that table via this very useful answer by Roi Gavish on a related question here.
Take, for instance, the following temporary table:
Let's see what the table contains:
The identity value can be inspected by this code:
For
BIGINT
identity columns, we need to expand the size of some variables used in the code, such as:Results for the
BIGINT
: