When should I use SET IDENTITY_INSERT <tablename> OFF function? - sql-server

To insert values into the identity column manually, I use:
SET identity_insert product ON
INSERT INTO product (PID, ProdName, Qty, Unitprice)
VALUES (10, 'soap', 5400, 22)
Firstly I have deleted the 10th row/record, then I have used this command to insert the identity value and record it manually.
this command is inserting the record. It's fine
Where should I write the command SET identity_insert product OFF? And what is the use of the SET identity_insert product OFF command?

From the documentation:
At any time, only one table in a session can have the IDENTITY_INSERT property set to ON.
So, if you are in a batch where you want to override the auto-generated identity values on two different tables, you would set the first one to OFF before setting the second one to ON.
(Also, like a lot of things, if you change something from the default, it's never a bad idea to change it back.)

from https://learn.microsoft.com/en-us/sql/t-sql/statements/set-identity-insert-transact-sql?view=sql-server-ver15
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 should set it off before you set it on to another table in your session.

Related

Is IDENTITY_INSERT session-specific?

Is IDENTITY_INSERT session-specific? Say there is an application using table "Test" and is entering some data in it. If i set IDENTITY_INSERT on and insert a row manually, will the application be able to keep on entering rows if it DOES NOT specify any PK?
Yes, Identity_insert is session specific. The next insert done by an application will use the most recent value as the starting point to calculate the next PK value.
See more at MSDN: https://msdn.microsoft.com/en-us/library/ms188059.aspx#Anchor_2
I think you confuse what IDENTITY_INSERT on means - you must manually specify a value. You must always specify a value for PK.

SQL Server : insert into fails because of entity insert is off

I can't figure out what I am doing wrong.
This is my code
SET IDENTITY_INSERT [Erea_Local].[Domain].[BlikAfmeting] ON
INSERT INTO [Erea_Local].[Domain].[BlikAfmeting]
SELECT *
FROM [dbo].[BlikAfmetings]
SET IDENTITY_INSERT [Erea_Local].[Domain].[BlikAfmeting] OFF
This is the error
An explicit value for the identity column in table 'Erea_Local.Domain.BlikAfmeting' can only be specified when a column list is used and IDENTITY_INSERT is ON.
If I only run the set IDENTITY INSERT .....
I get the message command completed
According the message:
An explicit value for the identity column in table
'Erea_Local.Domain.BlikAfmeting' can only be specified when a column
list is used and IDENTITY_INSERT is ON.
You should use a column list of values.

How to SUM to change every row record to a new value?

I am using Microsoft SQL Server Management Studio.
I want to sum each row of my record (say, column ID) with a constant value (say 50).
So here is a better picture:
Click here for the image that i prepared for better understanding of my requirement.
I know SUM can be used to sum up the entire column and come out with a TOTAL at the end of the row, but how about adding every row's record with a constant value?
Once the sum is done, I need to do my last step, which is to reset the ID of the next new row (which doesn't need to SUM like above) from 154 to 999. Can anyone also advice me on resetting primary key?
Dick,
You can change the identity column's next value easily by executing DBCC CheckIdent command
Unfortunately, there is not an easy way to update an identity column value.
For existing records, what I experienced till now is to copy all data into a new temp table and organize values on this helper table. Then drop existing table. As following step rename the temp table to original table name.
If you try to remove identity property of a column, the SSMS generates a script as follows
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_MyTable
(
id int NOT NULL,
value int NULL
) ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_MyTable SET (LOCK_ESCALATION = TABLE)
GO
IF EXISTS(SELECT * FROM dbo.MyTable)
EXEC('INSERT INTO dbo.Tmp_MyTable (id, value)
SELECT id, value FROM dbo.MyTable WITH (HOLDLOCK TABLOCKX)')
GO
DROP TABLE dbo.MyTable
GO
EXECUTE sp_rename N'dbo.Tmp_MyTable', N'MyTable', 'OBJECT'
GO
COMMIT
I know this does not sound an easy way to update identity column values.

Help needed with "Cannot insert explicit value for identity column" Error

I have a table Table1 on which I have created an insert/update trigger. This trigger insert records of inserts/updates on Table1 into Table1_history. To test whether the trigger is inserting records into Table1_history, I have inserted some test data into Table1 and I am receiving the error:
Cannot insert explicit value for identity column in table 'Table1_history' when IDENTITY_INSERT is set to OFF
Schema of Table1 and Table1_history are identical except an additional 'inserted_on_date' field in Table1_history.
I have set the Identity Insert to ON and the trigger works. But do I have to do this every time I need to insert a new record in Table1?
Put the IDENTITY INSERT ON into the trigger itself, it only needs to be active while you are pushing values into table1_history, because that is where the problem is.
Or, in Table1_history, don't make the pk field an IDENTITY, it does not need to be because you are supplying the values from Table1.
I assume your 'inserted_on_date' field is a datetime. You should be able to turn off the identity insert and set the default value for this column to GETDATE(), which will set any newly inserted records to the datetime the server has when the record is inserted.
You don't mention what database but I assume it's SQL Server 2008.
The set identity on command is session specific. So yes, you need to issue it each time.
Instead, you should remove the identity specification from the history table. You don't need it.

script to add and remove auto-increment property from a column

For a sql script I'm working on, I need to programmatically remove the identity, identity seed, and identity increment for a column in an existing table, then add them back to the table at the end of the script. Does anyone have a reference or an example on how to do this?
You should do this:
SET IDENTITY_INSERT <TableName> ON
-- Do the inserting in the table with name <TableName>
SET IDENTITY_INSERT <TableName> OFF
For more details look in the MSDN.
Yes, you just do this:
SET IDENTITY_INSERT [TABLE] ON
And then back on:
SET IDENTITY_INSERT [TABLE] OFF
This will allow you to enter manual data in the identity column.
http://msdn.microsoft.com/en-us/library/ms188059.aspx

Resources