Solved

IDENTITY_INSERT

Posted on 2004-08-09
16
1,588 Views
Last Modified: 2008-02-01
Greetings...can someone please help me resolve this problem (I'm new to SQL):

I have a table (RotaryDrilling) with an identity field (Record_Number) that uses

CREATE Trigger insert_key ON RotaryDrilling
FOR Insert AS
INSERT INTO RotaryDrillingCA (Record_Number) SELECT Record_Number FROM inserted
Order by Record_Number

to update Record_Number to a second table (RotaryDrillingCA). Using an Access (2000) form to input a new record, I get the following error:

ODBC--insert on a linked table 'dbo_RotaryDrilling' failed.
[Microsoft][ODBC SQL Server Driver][SQL Server]Explicit value must be specified for identity column in table 'RotaryDrilling' when IDENTITY_INSERT is set to ON. (#545)

I don't want to specify an explicit value...what do I need to do? This is very urgent.

Thanks...
0
Comment
Question by:rsanglim
  • 9
  • 3
  • 2
  • +1
16 Comments
 
LVL 26

Expert Comment

by:Hilaire
ID: 11753590
Most likely you have set IDENTITY_INSERT somewhere else in your code

If you want the identity field to be populated auotmatically, you'll have to issue a

SET IDENTITY_INSERT RotaryDrilling  OFF

statement
(I think the problem is not related to the trigegr itself)
0
 

Author Comment

by:rsanglim
ID: 11753657
I agree that the problem is not related to the trigger itself...I was trying to supply all the information that may be relevant. I'm not 'savvy' enough to have set IDENTITY_INSERT somewhere else...how do I set it to OFF?
0
 

Author Comment

by:rsanglim
ID: 11753731
e.g., where would I issue the OFF statement?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:SjoerdVerweij
ID: 11753816
Query Analyzer.
0
 

Author Comment

by:rsanglim
ID: 11754089
Using the Query Analyzer, can I execute code that issues the OFF statement each time a user wants to enter a new record via the form? Is that another INSERT trigger (or an addition to what I currently have)? My apologies, but this is all foreign to me.

0
 
LVL 26

Expert Comment

by:Hilaire
ID: 11754321
No, you should not have to set it to off whenever you need to issue an insert statement.

If you don't remember setting it to on, maybe you could search your code to find where it was set to on.

Basically this should be done only in rare occasions, since automatic incremental number is what identity fields are intended for.

Maybe yuo had to set it ON for a special loading process ??
0
 
LVL 5

Accepted Solution

by:
hkamal earned 500 total points
ID: 11754395
You can switch off IDENTITY_INSERT at db level;
SET IDENTITY_INSERT OFF
in your session or add it prior to the INSERT statement in your trigger ..

0
 

Author Comment

by:rsanglim
ID: 11754463
The only code I have is an INSERT/DELETE trigger(s). When I designed the table, I identified the field as PK, Identity=Yes, Seed = 1, Increment = 1. It occurs to me that this may be the source of my problem when I attempt to insert a new record.

The problem doesn't arise when inserting into the table but, rather, when I attempt to insert the record number into the foreign key of the associated table (Identity = No).
0
 

Author Comment

by:rsanglim
ID: 11754489
hkamal - I was just experimenting with your suggestion (add prior to the INSERT statement of my trigger) prior to my last post, and seem to be having some initial success.
0
 

Author Comment

by:rsanglim
ID: 11757254
Adding SET IDENTITY_INSERT OFF prior to the INSERT statement of the trigger has not resolved the problem...
0
 
LVL 18

Expert Comment

by:SjoerdVerweij
ID: 11757449
Okay, this

"Explicit value must be specified for identity column in table 'RotaryDrilling' when IDENTITY_INSERT is set to ON. (#545)"

That the trigger is not the problem. Please show how you are inserting (stored procedure?).
0
 
LVL 5

Expert Comment

by:hkamal
ID: 11757637
rsanglim, what happens when you do a manual insert :
INSERT INTO RotaryDrillingCA (Record_Number) SELECT 123456 (from QA)

And when you do it inside a proc:
create proc InsertTest as
  INSERT INTO RotaryDrillingCA (Record_Number) SELECT 123456 (from QA)
go
exec InsertTest
0
 

Author Comment

by:rsanglim
ID: 11757715
I'm sorry, hkamal - what is your reference to ""(from QA)"?

Also, I just ran a test on a PDA that pushes data into the 'master' table (RotaryDrilling). The data went in fine, and the associated Record_Number was inserted into the FK of the other table...no issues.
0
 

Author Comment

by:rsanglim
ID: 11757923
hkamal - I'm fairly convinced your original solution re: << add it prior to the INSERT statement in your trigger >> is the answer. However, even though I've included the statement in the INSERT trigger it seems as though it needs to be 'refreshed'. I ran the SET IDENTITY_INSERT OFF in the Query Analyzer, on the off chance it would work, and it did.

On one hand, that's good news. On the other, I shouldn't have to refresh every time a new record is inserted should I?
0
 

Author Comment

by:rsanglim
ID: 11759099
hkamal - thanks...I think this is going to get it.
0
 
LVL 5

Expert Comment

by:hkamal
ID: 11760624
OK good stuff !
I always assume people use Query Analyser and would know the shorthand "QA".
I asked you to test the manual parts as I was curious which part caused the problem (though adding SET IDENTITY_INSERT OFF  right before the insert would fix it anyway
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question