Reset Identity Columns for Tables Using Them

  • ckempste

    SSCoach

    Points: 17983

    Comments posted to this topic are about the item Reset Identity Columns for Tables Using Them


    Chris Kempster
    www.chriskempster.com
    Author of "SQL Server Backup, Recovery & Troubleshooting"
    Author of "SQL Server 2k for the Oracle DBA"

  • kuldipMCA

    Old Hand

    Points: 328

    but what about current data in the Table

    what we do for this data ?

    any solution because we set primary key for the identity column

    so what happned for that thing ?

  • Ron McCullough

    SSC Guru

    Points: 63877

    For a full understanding of the DBCC CHECKIDENT command read Books On Line (BOL) at:

    ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/2c00ee51-2062-4e47-8b19-d90f524c6427.htm

    paying particular attention to what might happed if you use the command improperly ...

    If the table is not empty, setting the identity value to a number less than the maximum value in the identity column can result in one of the following conditions:

    If a PRIMARY KEY or UNIQUE constraint exists on the identity column, error message 2627 will be generated on later insert operations into the table because the generated identity value will conflict with existing values.

    If a PRIMARY KEY or UNIQUE constraint does not exist, later insert operations will result in duplicate identity values.

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • kuldipMCA

    Old Hand

    Points: 328

    That's right but then

    what is the use of the set indentity ?

    if we set it to max no value in the table.

    so there no change in the table after or before this command.

  • ps.

    One Orange Chip

    Points: 29250

    kuldip.bhatt (1/20/2009)


    That's right but then

    what is the use of the set indentity ?

    if we set it to max no value in the table.

    so there no change in the table after or before this command.

    are you referring to SET IDENTITY_INSERT ON? This is used to explicitly insert a specific value in the identity column.

    Also what are you reffering to max value in the table? The max value is determined by the capacity of underlying datatype used for the identity column. If you use bigint instead of int, you can have more number of identity values.



    Pradeep Singh

  • kuldipMCA

    Old Hand

    Points: 328

    I know Indentity_Insert = On .

    my questiion is

    Suppose there is one table and it has one primary key it's type is Integer.

    currrenlty this table has 20 rows but it's last indentity value is 60.

    then i use this dbcc command or not use it will be insert the next row

    at 61 am right or wrong ?

    then what is the use of this DBCC COmmand that i my Question ?

  • ps.

    One Orange Chip

    Points: 29250

    kuldip.bhatt (1/20/2009)


    my questiion is

    Suppose there is one table and it has one primary key it's type is Integer.

    currrenlty this table has 20 rows but it's last indentity value is 60.

    then i use this dbcc command or not use it will be insert the next row

    at 61 am right or wrong ?

    then what is the use of this DBCC COmmand that i my Question ?

    Yes it'll get the value 61 (if the increment is set to 1).

    You can use dbcc to find the current identity value and current column value and reseed the current identity value to a new value of your choice or need.

    In your case, assuming the identity column has gaps (say 20,21,22,23) and you fire DBCC checkident('mytable', reseed, 20), the next time you try to insert any row in the table, it'll start with 21

    Pls go through the following test...

    create table #t

    (

    col1 bigint identity(1,1),

    col2 varchar(10)

    )

    -- run the below query 6 times

    insert into #t(col2)

    select 'a'

    union all

    select 'a'

    union all

    select 'a'

    union all

    select 'a'

    delete from #t where col1 between 20 and 25

    dbcc checkident("#t")

    --output

    Checking identity information: current identity value '29', current column value '29'.

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

    -- now reseeding the identity column

    dbcc checkident("#t",reseed,20)

    --output

    Checking identity information: current identity value '29', current column value '20'.

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

    -- again start inserting into the table

    it'll start from 21..... and will create duplicates if there are existing values.

    Hope this clarifies...



    Pradeep Singh

  • kuldipMCA

    Old Hand

    Points: 328

    Thanks I understand what u said ok

    so if Primary key then there no solution for the DBCC COmmand

    but you can insert duplicate value in the table.

    One more Question it that i had some problem

    facing

    i can't use Order by in Sub Query.

    is there any mechansium that i use order by desc and top in the sub Query

    if u know then tell me...

  • ps.

    One Orange Chip

    Points: 29250

    kuldip.bhatt (1/20/2009)


    so if Primary key then there no solution for the DBCC COmmand

    I dont think i understood what you mean here...

    i can't use Order by in Sub Query.

    is there any mechansium that i use order by desc and top in the sub Query

    You can use both ORDER BY and TOP in a sub query. You can also use only TOP in the subquery. You cannot use only ORDER BY in a subquery.

    Quoting from BOL

    When ORDER BY is used in the definition of a view, inline function, derived table, or subquery, the clause is used only to determine the rows returned by the TOP clause. The ORDER BY clause does not guarantee ordered results when these constructs are queried, unless ORDER BY is also specified in the query itself.



    Pradeep Singh

  • kuldipMCA

    Old Hand

    Points: 328

    Thanks for Support and answering my Question.

    i got the solution for the Order by in sub Query ok thanks.

Viewing 10 posts - 1 through 10 (of 10 total)

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