terça-feira, 17 de janeiro de 2012

Checking identity column overflow on all tables of a SQL Server database

The DMV (Dynamic Management View) query below can be used to get a list of all tables with an identity column, and the current value

SELECT o.name AS TableName,  c.name AS ColumnName,  t.name Datatype,  IDENT_CURRENT(o.name) CurrentID,  case t.xtype  when 48 then 127  when 52 then 32767  when 56 then 2147483647  when 127 then 9223372036854775807  else NULL  end MaximumID,  IDENT_CURRENT(o.name) * 100.0 /  case t.xtype  when 48 then 127  when 52 then 32767  when 56 then 2147483647  when 127 then 9223372036854775807  else NULL  end OccupationPrcFROM syscolumns c JOIN sysobjects o ON c.id = o.idJOIN systypes t ON c.xtype = t.xtypeWHERE  c.status & 0x80 = 0x80 -- 0x80 is an identity columnORDER BY 6 desc, 4 desc, 5
The query output contains these columns:


  • TableName
  • ColumnName: Column name of the identity column
  • Datatype: Datatype of the identity column
  • CurrentID: Current value of the identity column
  • MaximumID: Maximum value of the identity column
  • OccupationPrc: Percentage of IDs in use (100% * CurrentID / MaximumID).  When this column reaches 100, an overflow will happen.  The list is sorted descending on this column.
This query works for identity columns of datatypes tinyint, smallint, int and bigint.

Nenhum comentário:

Postar um comentário