Author |
Topic |
cray
Starting Member
9 Posts |
Posted - 2005-11-18 : 09:53:30
|
Created a new table within my software then went to Enterprise Mgr to set the one key field to be an identity field - so it could be an automatic unique identifier. When I try to enter data get error - IDENTITY INSERT SET TO OFF. Tried to do a Select *from dbo.tableSET IDENTITY INSERT table ONbut SQL did not understand INDETITYAny help appreciated.My accting software is set on their tables to sequence some tables by user entering 'new' and when you tab out of that field on a new record the software autosets the next sequential number. Want to do same on this table I'm trying to create. |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-11-18 : 09:56:33
|
SET IDENTITY_INSERT table ONInsert recordsSET IDENTITY_INSERT table OFFMadhivananFailing to plan is Planning to fail |
 |
|
cray
Starting Member
9 Posts |
Posted - 2005-11-18 : 10:01:46
|
table is not a user table. Cannot perform SET operation.What does this mean???? Receive this error after attempting to set identity_insert on. |
 |
|
SamC
White Water Yakist
3467 Posts |
Posted - 2005-11-18 : 11:09:36
|
SET IDENTITY_INSERT [your table name goes here] ON |
 |
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2005-11-18 : 22:32:57
|
You might want to check out Books Online. It's included free with SQL Server. Here's a quote from it:quote: Transact-SQL Reference SET IDENTITY_INSERTAllows explicit values to be inserted into the identity column of a table.SyntaxSET IDENTITY_INSERT [ database. [ owner. ] ] { table } { ON | OFF }ArgumentsdatabaseIs the name of the database in which the specified table resides.ownerIs the name of the table owner.table Is the name of a table with an identity column.RemarksAt any time, only one table in a session can have the IDENTITY_INSERT property set to ON. If a table already has this property set to ON, and a SET IDENTITY_INSERT ON statement is issued for another table, Microsoft® SQL Server™ returns an error message that states SET IDENTITY_INSERT is already ON and reports the table it is set ON for.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.The setting of SET IDENTITY_INSERT is set at execute or run time and not at parse time.PermissionsExecute permissions default to the sysadmin fixed server role, and the db_owner and db_ddladmin fixed database roles, and the object owner.ExamplesThis example creates a table with an identity column and shows how the SET IDENTITY_INSERT setting can be used to fill a gap in the identity values caused by a DELETE statement.-- Create products table.CREATE TABLE products (id int IDENTITY PRIMARY KEY, product varchar(40))GO-- Inserting values into products table.INSERT INTO products (product) VALUES ('screwdriver')INSERT INTO products (product) VALUES ('hammer')INSERT INTO products (product) VALUES ('saw')INSERT INTO products (product) VALUES ('shovel')GO-- Create a gap in the identity values.DELETE products WHERE product = 'saw'GOSELECT * FROM productsGO-- Attempt to insert an explicit ID value of 3;-- should return a warning.INSERT INTO products (id, product) VALUES(3, 'garden shovel')GO-- SET IDENTITY_INSERT to ON.SET IDENTITY_INSERT products ONGO-- Attempt to insert an explicit ID value of 3INSERT INTO products (id, product) VALUES(3, 'garden shovel').GOSELECT * FROM productsGO-- Drop products table.DROP TABLE productsGOSee AlsoCREATE TABLEIDENTITY (Property)INSERTSET©1988-2000 Microsoft Corporation. All Rights Reserved.
MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
ChrisB
Starting Member
6 Posts |
Posted - 2005-11-29 : 20:32:31
|
I've run into the same problem. Sometimes it works, but most of the time it doesn't. I found this on MS: http://support.microsoft.com/default.aspx?scid=kb;en-us;878501It says that SP4 fixes the problem, so I installed it, but I'm still getting the error. If anyone finds out something, please let me know.Thanks,Chris |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-11-29 : 20:40:42
|
Chris,Please post the code that you are using that is giving you the error.Tara Kizeraka tduggan |
 |
|
ChrisB
Starting Member
6 Posts |
Posted - 2005-11-29 : 21:28:18
|
I'm doing this within a stored procedure, looping through a number of tables. I experimented in QA using the below:declare @PKTable nvarchar(100)set @PKTable = 'tblActGroup'SET IDENTITY_INSERT @PKTable ONexec('SET IDENTITY_INSERT ' + @PKTable + ' ON')exec('SET IDENTITY_INSERT tblActGroup ON')SET IDENTITY_INSERT tblActGroup ONSET IDENTITY_INSERT tblActGroup OFFSET IDENTITY_INSERT tblActGroupDet ONSET IDENTITY_INSERT tblActGroupDet OFFI tried different combinations. The hard-coded commands always worked as expected. The exec('...') commands would say they executed successfully, but in fact they didn't.I've read the BOL carefully, but can't figure out why the same command behaves differently.Thanks,Chris |
 |
|
ChrisB
Starting Member
6 Posts |
Posted - 2005-11-29 : 21:30:09
|
Oops, please ignore the line:SET IDENTITY_INSERT @PKTable ONI tried it just to see if it would work; it didn't. |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-11-30 : 12:32:08
|
You can't do IDENTITY_INSERT dynamically. Dynamic SQL exists in its own session, so you aren't able to get to it once you run the next command. You will have to hard code the table names.Tara Kizeraka tduggan |
 |
|
Palinmaj
Starting Member
1 Post |
Posted - 2011-01-10 : 10:34:41
|
or try using the old trick. DECLARE @StrSql as varchar(100)set @StrSql = 'SET IDENTITY_INSERT ' + @PKTable + ' OFF'exec (@strsql)i tried it and works fine. |
 |
|
|