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.
Related
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 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.
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.
I do a unit test Add/Insert a data record into a table with column name Id which is an identity column.
In the unit test I manually insert the id. So I could set the Identiy_Insert to ON before the Insert statement.
Are there any drawbacks keeping this feature turned ON for the production server?
Or should I change my unit testing approach? Inserting a value for the Name field and retrieve
and ASSERT it should be prove enough that the data record got inserted for the integrations
sake.
its not a feature you can keep ON. It is valid per connection.
give it a try, open a connection on SSMS and set it to ON to one table.
Then open another connection and you will see that if you try to insert the IDENTITY on the same table, it will fail.
From MSDN:
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.
In general, I would not recommend using IDENTITY_INSERT in production to avoid concurrency issues.
As for the unit tests, I'd suggest creating a mocked data access layer instead of relying on a real database.
I had an unusual problem yesterday where I was suddenly unable to insert records into a table with an identity column.
A simple insert like this: INSERT INTO MyTable (Column1, Column2) VALUES ('text', 236764)
Started throwing a primary key constraint violation.
I ran DBCC CHECKIDENT on the table, and realized that SQL Server had stopped updating the last used value, so that when it was inserting it was incrementing using the old value and the new identity value usually already existed in the table, hence the violation errors.
Resolving the problem wasn't an issue, I just reseeded the table for the next highest sequence number, but I've never seen this happen before!
Does anyone have any idea what might cause SQL Server to stop updating identity properties, and where I might look for evidence? There is no replication or any triggers involved, it's just a plain old table.
EDIT: SQL Log Rescue would have been ideal, but it only works on SQL Server 2000. Is there a similar tool out there for SQL 2005 logs?
If someone has inserted to the table using SET IDENTITY_INSERT ON, someone could absolutely have entered in an invalid value for the table. That would be my first guess. You could use a log analyzer like SQL Log Rescue to go back in time through the transaction logs and see if you could find who the bad person was who messed up your data...
I think SET IDENTITY_INSERT ON reseeds the Identity.
From BOL
If the value inserted is larger than
the current identity value for the
table, SQL Server automatically uses
the new inserted value as the current
identity value.
The only way I could reproduce this issue was to manually set the seed too low with DBCC CHECKIDENT.