Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Reset Identity Columns for Tables Using Them Expand / Collapse
Author
Message
Posted Sunday, September 23, 2007 5:22 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, July 31, 2007 8:20 AM
Points: 885, Visits: 1
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"
Post #401561
Posted Tuesday, January 20, 2009 7:23 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 5:06 AM
Points: 22, Visits: 106
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 ?
Post #639944
Posted Tuesday, January 20, 2009 4:04 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 4:50 PM
Points: 5,574, Visits: 24,823
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

Before posting a performance problem please read
Post #640301
Posted Tuesday, January 20, 2009 9:31 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 5:06 AM
Points: 22, Visits: 106
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.


Post #640380
Posted Tuesday, January 20, 2009 10:36 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, July 6, 2014 3:07 PM
Points: 2,242, Visits: 3,643
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
Post #640405
Posted Tuesday, January 20, 2009 11:07 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 5:06 AM
Points: 22, Visits: 106
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 ?
Post #640425
Posted Tuesday, January 20, 2009 11:27 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, July 6, 2014 3:07 PM
Points: 2,242, Visits: 3,643
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
Post #640434
Posted Tuesday, January 20, 2009 11:40 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 5:06 AM
Points: 22, Visits: 106
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...
Post #640443
Posted Tuesday, January 20, 2009 11:51 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, July 6, 2014 3:07 PM
Points: 2,242, Visits: 3,643
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
Post #640447
Posted Wednesday, January 21, 2009 2:16 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 5:06 AM
Points: 22, Visits: 106
Thanks for Support and answering my Question.

i got the solution for the Order by in sub Query ok thanks.
Post #640510
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse