Solved

SET IDENTITY_INSERT CI_Equipment ON ? sql server 2008

Posted on 2011-02-22
8
353 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
Industry Leaders: 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!

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

739 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