Cannot insert explicit value for identity column in table 'XXX' when IDENTITY_INSERT is set to OFF

Cannot insert explicit value for identity column in table 'XXX' when IDENTITY_INSERT is set to OFF

book

Article ID: KB0071062

calendar_today

Updated On:

Products Versions
TIBCO ActiveMatrix BusinessWorks Plug-in for Database -

Description

The following error occurs when attempting to insert a value into an identity column on Microsoft SQL Server.
======================================================
Cannot insert explicit value for identity column in table 'XXX' when IDENTITY_INSERT is set to OFF
======================================================
 

Issue/Introduction

Cannot insert explicit value for identity column in table 'XXX' when IDENTITY_INSERT is set to OFF

Environment

Product: TIBCO ActiveMatrix BusinessWorks Plug-in for Database Version: All supported versions OS: All Supported Operating Systems

Resolution

The value of an identity column should be automatically generated by the database. However, sometimes there is a request to manually insert data into an identity column. When inserting a data directly into an identity column, the above error occurs.

There are two workarounds to solve the problem:

1 When using the TIBCO Database driver, we can add "EnableReplicationUser=true" to the JDBC URL. For example:
jdbc:tibcosoftwareinc:sqlserver://<host>:<port>;DatabaseName=<dbname>;EnableReplicationUser=true

Note that this property only works with the TIBCO Database driver, and it requires TIBCO(R) Database Drivers Supplement(TM) 2.0.6_HF-004 or above. It also requires the IDENTITY columns to be defined as NOT FOR REPLICATION. For more information about this property, see the following driver document:

https://docs.progress.com/bundle/datadirect-microsoft-sql-server-jdbc-60/page/EnableReplicationUser.html

2 Create a stored procedure to set the IDENTITY_INSERT to on when inserting the data. Then use the DataRequester activity (8.x) or the Request-Response Service (7.x) to insert the data. 
The following is an example of the stored procedure:
=====================================
CREATE PROCEDURE <example>
AS
Begin
    set IDENTITY_INSERT <table> on
    insert statement
    set IDENTITY_INSERT <table> off
End
GO
=====================================

Additional Information

https://docs.progress.com/bundle/datadirect-microsoft-sql-server-jdbc-60/page/EnableReplicationUser.html