Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SET IDENTITY_INSERT CI_Equipment ON ? sql server 2008

Posted on 2011-02-22
8
Medium Priority
?
362 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
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…

971 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