Solved

SET IDENTITY_INSERT CI_Equipment ON ? sql server 2008

Posted on 2011-02-22
8
357 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 143

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
Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

 

Author Comment

by:contel
ID: 34951095
Thank you very much!!
0
 
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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

623 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