How do you UPDATE an identity column in SQL azure? Is it possible? - azure-sql-database

I have this code:
ALTER TABLE FishSticks
ADD MyNewIdentityColumnId INT IDENTITY(1,1) NOT NULL
SET IDENTITY_INSERT FishSticks ON
UPDATE FishSticks
SET MyNewIdentityColumnId = MyOldColumnId
However it says:
Cannot update identity column 'MyNewIdentityColumnId'.
I assume this is due to SQL Azure, but I didn't have much success googling. It seems that changing a column to be an identity while preserving data is a huge difficulty.

SET IDENTITY_INSERT only works for INSERTS and that error is not specific to SQL Azure. You'll get the same error on SQL Server/Express
to go around the issue, SET IDENTITY_INSERT to ON, reinsert all the column values from the existing row whose identity value you want to replace with a new value, SET IDENTITY_INSERT OFF, then delete the previous row.

I found this Possible solution from Herve Roggero who basically gives a way of turning off the ID and then adding the rows, then turning ID back on.

Related

is it possible to insert data to a table with a specific primary key even if it is identity in SQL server [duplicate]

This question already has answers here:
How to turn IDENTITY_INSERT on and off using SQL Server 2008?
(7 answers)
Closed 2 years ago.
I'm trying to import an SQL Server database to mylittleadmin but for some reason, it not showing me all it options like import backup or any kind of importing method maybe it's due to permissions of my web hosting (GoDaddy).
So now am trying to insert all the data using SQL queries I already converted all my database to script but almost all tables have an identity primary key so when I tried to execute the script.
I got this error:
Cannot insert explicit value for identity column in table 'Adversaire'
when IDENTITY_INSERT is set to OFF.
This is a three-step process:
Turn IDENTITY_INSERT on for the table you want to insert data into. ex:
SET IDENTITY_INSERT Adversaire ON
Insert your data into that table, explicitly providing values to the identity field.
INSERT INTO Adversaire (name_of_your_identity_field, other_field1, other_field2...) etc...
When you're done inserting that data, be sure to turn IDENTITY_INSERT back OFF.
SET IDENTITY_INSERT Adversaire OFF
because according to the docs: At any time, only one table in a session can have the IDENTITY_INSERT property set to ON.
If the field is a Primary Key or otherwise has uniqueness constraints on it, turning on IDENTITY_INSERT still won't let you slip in duplicates. If you need to replace records that were already assigned a given ID value, you will either need to delete the old ones before inserting, or update the old records, whichever makes more sense in your application.

SQL Server - can I let the IDENTITY_INSERT as ON forever?

I have a super big table, more than 404 million rows, with an Identity column, but we don't use that table anymore, we only send certain data to it, is it possible to execute only once the SET IDENTITY_INSERT ON and never need to use it again?
What I want is to avoid any failure by the identity column during the inserts.
If I execute only that line it will remain forever or it's by session?
Thanks
https://learn.microsoft.com/en-us/sql/t-sql/statements/set-identity-insert-transact-sql?view=sql-server-ver15 says :
At any time, only one table in a session can have the IDENTITY_INSERT
property set to ON. If a table already has this property set to ON,
and a SET IDENTITY_INSERT ON statement is issued for another table,
SQL Server returns an error message that states SET IDENTITY_INSERT is
already ON and reports the table it is set ON for.
So, you'll have to set the identity_insert on every new session, and you're limited to only one table at a time per session.
You'll be better fixing the table, especially if "you don't use that table anymore". If the identity doesn't matter anymore, remove it.

Append existing data to table with identity

SQL Server 2014: I am importing a table that is living in an old SQL Server to a new server. What I did was download the table to MS Access, then uploaded it to the new server (different environments).
The problem is that on upload, my primary key and auto increment are dropped. PK was easy to fix, and I was able to add a new identity column, but now I cannot append to the identity column as an error saying IDENTITY_INSERT is ON. So I turned it off, but still getting the same error.
Any ideas work a workaround?
Reading the documentation for INDENTITY_INSERT:
At any time, only one table in a session can have the IDENTITY_INSERT property set to ON. If a table already has this property set to ON, and a SET IDENTITY_INSERT ON statement is issued for another table, SQL Server returns an error message that states SET IDENTITY_INSERT is already ON and reports the table it is set ON for.
If you already tried turning it off for the other table, you might be in a transaction - try issuing a COMMIT.

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 force SQL Server 2008 to not change AUTOINC_NEXT value when IDENTITY_INSERT is ON?

I got question about IDENTITY_INSERT. When you change it to ON, SQL Server automatically changes AUTOINC_NEXT value to the last inserted value as identity.
So if you got only one row with ID = 1 and insert row with ID = 100 while IDENTITY_INSERT is ON then next inserting row will have ID = 101. I'd like it to be 2 without need to reseed.
Such behaviour already exists in SQL Server Compact 3.5. Is it possible to force SQL Server 2008 to not change AUTOINC_NEXT value while doing insert with IDENTITY_INSERT = ON ?
I'm not aware of any way to prevent this behavior - after all, it's a prudent thing to do, anyway! If you've already set a value x (whatever that value might be), you shouldn't leave your seed value lower than x since otherwise, you're bound to run into getting a value in your IDENTITY column that's already there - not a good place to be!
But if you must, you can re-seed your IDENTITY column after you're done with your inserts using:
DBCC CHECKIDENT ('YourTableName', RESEED, 300);
where you can set any arbitrary value as your new seed value (here: 300). Of course, you need to be extra careful not to create any duplicates by setting the reseed value too low.
Additionally, if you do a reseed and your identity column is also a primary key, when you get back to the originally inserted value when IDENTITY_INSERT tablename ON was set you will get a PK violation. Another thing to think about.
Why would you want to do that? In the first place, there should be almost no occasions where you set identity insert to ON in a production system. And what difference would it make if you skip some? You can't count on identities not skipping in any event since it will skip if you do a rollback.