DBCC CHECKIDENT

  • Hi,

    Does anyone know if it's possible to insert the results of DBCC CHECKIDENT into a table?

    Thanks

    Dave

  • If you want to insert the IDENTITY value of a table into another table, there are other ways you can do it. One such way would be to use the IDENT_CURRENT() function

    INSERT SomeTableName( TableName, IdentityValue )

    SELECT 'TableName', IDENT_CURRENT( 'TableName' )

    Hope this helps


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Thanks for the reply, Unoforunatly that won't give me what I need.

    I need to compare the results of the dbcc checkident on 767 tables. It looks like as part of an upgrade from 200 to 2008 the seeding has gone out on sync,

    so ideally i'd want to try and insert the result from the dbcc command so far i've built this qury up. but it's only running the first table, does anyone know how to make it loop through all the tables for dbcc cmd?

    DECLARE @sqlstring nvarchar(500);

    DECLARE @tableNo int

    DECLARE @name nvarchar(50);

    DECLARE @ParmDefinition NVARCHAR(500);

    Set @sqlstring =N'

    SELECT @dbcccmdout =

    ''DBCC CHECKIDENT ('''''' + name +'''''');''

    FROM

    sys.objects

    WHERE

    OBJECTPROPERTY(OBJECT_ID(name),''TableHasIdentity'') = 1

    AND

    TYPE =''U''';

    SET @ParmDefinition = N'@dbcccmdout varchar(1000) OUTPUT';

    execute sp_executesql @sqlstring,@ParmDefinition,@dbcccmdout = @name output;

    exec (@name)

  • You could do it in this way then

    INSERTSomeTableName( TableName, IdentityValue )

    SELECTname, ident_current( name )

    FROMsys.objects

    WHEREOBJECTPROPERTY( OBJECT_ID( name ),'TableHasIdentity' ) = 1

    ANDTYPE = 'U'


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Thanks for the reply,

    The thing is I need the vaules current identity value and the current column value.

  • I am not sure but i think DBCC CHECKIDENT( 'TableName' ) always gives the same value for current column value and current identity value.

    It changes only when you reseed it to a particular value. Does it change under any other condition..


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • I'm afraid it doesn't

    DBCC checkident ('tblxxxxxxxxxxxxx')

    Checking identity information: current identity value '23935390', current column value '23935403'.

    DBCC execution completed. If DBCC printed error messages, contact your system administrator.

  • It changes when the database is restored from sql 200 to 2008,

    I've run upgrade advisor and data purity checks and set the compatability mode to 100, so all bases covered - just can't explain why the seeding on the tables have gone to pot!

  • Well, thats a bit of a problem. I have no idea of how to solve this. Hopefully somebody will come up with a solution soon.

    As for the problem in your code i think it is in this block

    Set @sqlstring =N'

    SELECT @dbcccmdout =

    ''DBCC CHECKIDENT ('''''' + name +'''''');''

    FROM

    sys.objects

    WHERE

    OBJECTPROPERTY(OBJECT_ID(name),''TableHasIdentity'') = 1

    AND

    TYPE =''U''';

    It should actually be

    Set @sqlstring =N'

    SELECT @dbcccmdout = COALESCE( @dbcccmdout + '';'', '''' ) +

    ''DBCC CHECKIDENT ('''''' + name +'''''')''

    FROM

    sys.objects

    WHERE

    OBJECTPROPERTY(OBJECT_ID(name),''TableHasIdentity'') = 1

    AND

    TYPE =''U''';


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply