Friday, February 24, 2012

Cannot insert explicit value for identity...

I am developing an integration process between two databases. One of
them is a SQL Server 2000 and the other is using MSDE 2000. The
integration process is done in C# (VS2003).

The main database is the SQL Server, the MSDE will contain a really
small subset of the data found on the main. To help diminish the amount
of time taken to develop an integration process between those
databases, the same structure are found on both side. The only
difference, when I insert data in the MSDE from the SQL Server, I set
the IDENTITY_INSERT to ON and use the same IDs found on the SQL Server.

I can insert one set of data without problem, but from there, if I try
again, I will always receive the "Cannot insert explicit value for
identity column in table ... when IDENTITY_INSERT is set to OFF." I
saw on Microsoft website the article ID 878501; I noticed I was using
MSDE sp3, I upgraded to SP4... and I still have the problem.

I know, when I call the update function on the sqldataadapter, the
adapters contain the IDENTITY_INSERT ON and it's set to OFF after the
insert. The "Cannot insert..." error is the only one I received.

Can anyone help me on that issue? Take note that this approach was
used because of customer requirements; the size of the database also
causes some problem (over 200 tables) and we decided to use the same
structure on both side to minimize the support time.Just thought about posting the SQL command in the adapter, it will look
something like:

SET IDENTITY_INSERT myTable ON; INSERT INTO myTable (myId, myCol1)
VALUES (@.myId, @.myCol1); SELECT myId, myCol1 FROM myTable WHERE (myId =
@.@.IDENTITY); SET IDENTITY_INSERT myTable OFF;

I hope that will help !|||(nicolas.bouchard@.gmail.com) writes:
> I am developing an integration process between two databases. One of
> them is a SQL Server 2000 and the other is using MSDE 2000. The
> integration process is done in C# (VS2003).
> The main database is the SQL Server, the MSDE will contain a really
> small subset of the data found on the main. To help diminish the amount
> of time taken to develop an integration process between those
> databases, the same structure are found on both side. The only
> difference, when I insert data in the MSDE from the SQL Server, I set
> the IDENTITY_INSERT to ON and use the same IDs found on the SQL Server.

The first reaction is: have you considered replication?

The second reaction is: skip IDENTITY, and generate the IDs on your
own. That is a trivial business. It's only if you have a high INSERT
rate from simultaneous processes that you need IDENTITY, as rolling
your own key can result in contention issues.

IDENTITY is convenient at times, but not in a situation like this. It
causes more problems than it solves.

Yet, an alternative is to use bulk load to extract and load data.
Moving one at time as you do not is not terribly effecient. Which may
not matter if the data size is moderate, but for something like
10-20 MB it could. With BCP it's easy to insert explicit IDENTITY
values with the -E option.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Hi,

thanks for the reply!

We did considered replication but for a reason unknown to me, we didn't
choose that solution; I can still imagine why, due to the fact we had
to develop an ownership process on certain tables, some tables are
owned by the main and others by the MSDE system and again, depending on
the state of the system.

The system (the main one) will be used by 200+ persons during the day,
the insert rate may be quite a problem with our own generated ids.

Until few days ago, I didn't know of bulk load, I will definitely look
into that for future projects. For this project, the amount of data
transfered between databases are not that big (talking less than 1MB
for the first transfer, then it will be almost nothing).

About my problem, I fixed it and I had few more tests to do; I was
planning to post the fix to my problem here, hoping it will help
someone else. I tried to do too much instructions in one command (my
guess is). Instead of doing everything in the same SQLCommand, I
modified the command to only manage the insert and the select to get
the new values; before calling the update command of the DataAdapter, I
disable the IDENTITY_INSERT then after the comand I enable it.

As I said, thanks for the quick answer !

Nicolas Bouchard

No comments:

Post a Comment