Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Auto number

Author  Topic 

nkpriya
Starting Member

34 Posts

Posted - 2011-07-20 : 14:43:11
Desgined a table with identity 'Yes' for autogenerating a column. It is working fine. As per my desired functionality, I need to drop that table and create it in the process. But, I need same values to be generated/retained for that column. Is there a way to do so?
Eg:
There is a test table:
ID Name
1 Ex1
2 Ex2
3 Ex3

Id field is autogenerated.
I am deleting all rows in the test table and inserting new rows. when I am inserting new rows, test table is generating values as 4,5,6. But, I need test table to generate the very first ID's like 1,2,3. Any ideas? Thanks

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-07-20 : 14:52:54
You can:

1. Use SET IDENTITY_INSERT to insert explicit values in an identity column: http://msdn.microsoft.com/en-us/library/ms188059.aspx
2. Use DBCC CHECKIDENT to reseed the identity value: http://msdn.microsoft.com/en-us/library/ms176057.aspx
3. Use TRUNCATE TABLE to empty the table and reset the identity back to the initial seed. This will not work if the table is referenced by a foreign key however.
Go to Top of Page

nkpriya
Starting Member

34 Posts

Posted - 2011-07-20 : 15:04:52
Thank you. I have a question though.
I am deleting all rows of a table in SSIS package using OLEDB command and then inserting values in the table from another source. How can I achieve the identity_insert functionality in this process? Please let me know. Thanks
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-07-20 : 15:25:56
On the OLE DB Destination Editor panel, under the Connection Manager pane, there's a checkbox for Keep Identity. Check that and you'll enable SET IDENTITY_INSERT on that destination table.
Go to Top of Page
   

- Advertisement -