SQL Server : alter the Identity seed - sql

I am migrating data from one database to another. I have my scripts mostly together already, but I am trying to figure out the best way to make one change to a table in the new database.
I have a Customer table. That table has a customer_id column which is the identity column. I want to change the identity seed/increment from (1,1) to (200,1) without changing the customer_ids for the existing data I will be inserting into the table.
Old data is 101-108. Basically we want to keep the old data the same so it matches up with old records in other systems, but we want the new data to start seeding in at 200.
I tried Googling how to do this, but all my Googling came back with results where people wanted to change what column was the identity column, and not just change the identity seed number. Is there a simple query I can use to accomplish what I want to do?

You can use DBCC CHECKIDENT:
DBCC CHECKIDENT ('dbo.customer', RESEED, 200)
This will change the current seed value of the identity column of the specified table. If you need to insert specific identity values, you can SET IDENTITY_INSERT ON in your insert statement.
IDENTITY_INSERT

What I would do unset the new column as an identity (using alter table), then insert the data from the old table, and then reset the new column as the identity again, with whatever increment you want as per the link
https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql-identity-property?view=sql-server-2017

Related

SQL set identity and retain values

I have a problem restoring a table.
I wanted to restore data by copying data from the backup to the table.
The problem is, the PK of the table has the Identity-Property set. So when I inserted the lost rows they got new IDs.
I created a new table without identity and put the data in there.
Now I want to turn on Identity on the PK column, which doesn't work.
Any what I can do or if this is possible at all?
You cannot add IDENTITY property to existing column. Either you insert in existing table with IDENTITY column with IDENTITY_INSERT ON' option or you create a newIDENTITY` column in the new table.
This post might help you
This will solve your problem
[https://stackoverflow.com/a/1049305/6652909]
If you have backup of table then TRUNCATE your table then set primary key then make it auto increment in table and then copy your table and make sure your data of table is entered properly, it worked for me may be it will helpful to you also.

How to reseed identity in Amazon redshift and how to make it auto increment

I am wondering how can we reset/reseed an identity column in Amazon redshift
I also want to make an auto increment column in my table.
You can do something like this while creating the table.
create table test(id bigint identity(1,1))
Adding identity column in existing table is not possible, so you need to re-create a new table and then copy your data from existing table. While re-creating you can reset the identity as column as required.
I too had the similar scenario to rest the Identity column in Redshift especially when the table is truncated. Unfortunately I couldn't find any option to reset the identity column. Using the work around of drop and create the table whenever required to rest but this many not work in all the scenarios.

How do I reorder or auto-increment the primary key of a table if it is not being incremented properly?

I inherited a SQL Server database and for some reason the primary key is not being incremented properly.
For example, the PK for the last record in the table is 120350
Then, when I enter a new record into the table, it gets the PK 48377
What would be the best way to reorder or set the initial PK for new records? This is for Microsoft SQL Server 2008 R2.
DBCC CHECKIDENT('<tablename>');
This will reseed the identity column to where it should be (in this case 120351). If you want to reorder the existing value then you'll need to use SET IDENTITY_INSERT '<tablename>' ON as part of a script to move the rows around. How you do that depends on what exactly you're trying to accomplish. Keep in mind that you'll also need to keep any foreign keys in sync.
If you have some other requirements on the column that defines, "incremented properly" then you might need something besides an IDENTITY column.
When I use
DBCC CHECKIDENT('UPS_Worldship_Export');
I got this result: Checking identity information: current identity value '48377', current column value '120351'.
Somehow, the value was offtrack. Thanks to the comments above, I just reseeded the value to the highest value and it works correctly now.
DBCC CHECKIDENT (',<table name>', RESEED, <current column value>);

Enable an identity column that already has data in it?

Someone disabled the identity column of a table in a SQL DB. Is it possible to re-enable that feature of the column even when there is already data in the table? And maintain the existing identity values?
I know I could copy the data to another table and reinsert it after setting Identity_Insert on.
You cannot switch on the IDENTITY on an existing column, that's just not possible in SQL Server right now (at least up to version 2012).
What you need to do is exactly what you describe:
create the new table in the structure you want, with the IDENTITY column
copy the data from the existing table into the new table, with SET IDENTITY_INSERT ON
drop the old table
rename the new table to the old table name
You can "re-enable" the identity specification in the visual table designer in SQL Server Mgmt Studio, but this really only does those above steps in the background, for you.
Just as *marc_s* said you can use
I dont know if there is any other way to do this but you can use
CREATE TABLE tblNewTable
(
//Put the columns and datatypes of the former table
)
INSERT INTO tblNewTable
AS
SELECT * FROM oldTable
Then drop the table usng
DROP TABLE oldTable
Then recreate the new table and add the identity column, then use
INSERT INTO tblNewRecreatedTable (//Columns of the new created table except the column with the identity
AS
SELECT //Columns of the table you copied the data to except the Columned that you defined identity
I hope it helps

How do I create a trigger to insert a value into an ID field that is Max([ID Field])+1 on insert

When I add a new record I want SQL Server to automatically add a fresh ID.
There are already some records which have been migrated over (from Access) and until I finish preparing the server for other required functionality I will be manually migrating further records over (if this affects any possible answers).
What are the simplest ways to implement this.
The simplest way would be to make the column an IDENTITY column. Here is an example of how to do this (it's not as simple as ALTER TABLE).
Make use of the Identity field type. This will automatically create a value for you using the next available number in the sequence.
Here is an example of how to create an Identity column (add a new column) on an existing table
ALTER TABLE MyTable ADD IdColumn INT IDENTITY(1,1)