Solved

SET IDENTITY_INSERT CI_Equipment ON ? sql server 2008

Posted on 2011-02-22
8
342 Views
Last Modified: 2012-05-11
Hello,
i am trying to execute the command
SET IDENTITY_INSERT CI_Equipment ON
On a table named CI_Equipment, it works well...Command(s) completed successfully.

The problem is when i try to insert a new row

INSERT INTO [CI_TevaAbic].[dbo].[CI_Equipment]
           ([EquipmentId]
           ,[EquipmentName]
           ,[EquipmentTypeId]
           ,[RegionId]
           ,[TimeCreated])
     VALUES
           (1
           ,'name'
           ,1
           ,1
           ,'01-01-1900'
           )
GO


it still shows me
Cannot insert explicit value for identity column in table 'CI_Equipment' when IDENTITY_INSERT is set to OFF.

What can I do?
0
Comment
Question by:contel
8 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 34951007
you have to run the insert on the same database, and on the same session

USE [CI_TevaAbic]
GO
SET IDENTITY_INSERT [dbo].CI_Equipment ON
INSERT INTO [dbo].[CI_Equipment]
           ([EquipmentId] 
           ,[EquipmentName]
           ,[EquipmentTypeId]
           ,[RegionId]
           ,[TimeCreated])
     VALUES
           (1
           ,'name'
           ,1
           ,1
           ,'01-01-1900'
           )
SET IDENTITY_INSERT [dbo].CI_Equipment OFF
GO

Open in new window


0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 34951028
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.

try with unused value for id
0
 
LVL 7

Expert Comment

by:waltersnowslinarnold
ID: 34951077
Use the following sample, it'll work.
USE [CI_TevaAbic]
GO
SET IDENTITY_INSERT [CI_TevaAbic].[dbo].CI_Equipment ON
INSERT INTO [CI_TevaAbic].[dbo].[CI_Equipment]
           ([EquipmentId] 
           ,[EquipmentName]
           ,[EquipmentTypeId]
           ,[RegionId]
           ,[TimeCreated])
     VALUES
           (1,'name',1,1,'01-01-1900')
SET IDENTITY_INSERT [CI_TevaAbic].[dbo].CI_Equipment OFF
GO

Open in new window

0
 

Author Comment

by:contel
ID: 34951095
Thank you very much!!
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 34951489
Please set identity on
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 34957432
waltersnowslinarnold,

Did you just copy and paste the solution posted by angelIII here http:#34951007?
0
 

Expert Comment

by:DonaldMarkSanders
ID: 35203355
Guys I was banging my head against the wall trying to get this to work and nothing worked at all.  I did everything you guys talked about above and it DID NOT WORK AT ALL.  Until I did the following:

For MS SQL 2008 and using Microsoft SQL Management Studio I had to click Tool - Options -  Designers and then UNCHECK "Prevent saving changes that require table recreation".  I saved the option and then went to my table changed the field to a non-identity field and then added the records I needed and then changed it back when I was done.

IT WORKED!
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 35203565
>>Guys I was banging my head against the wall trying to get this to work and nothing worked at all.  <<
The author is not using the Design mode to change the IDENTITY attribute.  All they want to do is explicitly INSERT the value into a column with an IDENTITY column, which is perfectly doable using the code angelIII posted provided:
1. They have the appropriate permissions.
2. They do not violate any constraint.

So your point is somewhat irrelevent as the "Prevent saving changes that require table recreation" checkbox only applies to changing the table schema using "Table and Database Designers" as shown in the same tab under Options.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now