Turning Off and On Identity Column in SQL Server

Posted April 21, 2006 at 2:05 PM by Ben Nadel

Tags: ColdFusion, SQL

I have been moving over the data from one database to another and am currently using XML files as an intermediate step. The process has been going very smoothly, and I just learned something new. I need to keep certain ID column values across the database and was worried about turning on and off identity column AND automating the XML to DB scripting. But, found a great solutions.

SET IDENTITY_INSERT tablename ON
SET IDENTITY_INSERT tablename OFF

It's wicked cool. But, it actually took me a bit to debug. I had the ON/OFF confused. I thought they were referring to the DB's insert functionality, and so, I thought I had to turn it OFF in order to insert my own values. I realize now, that it is referring to the SQL statements ability to insert identity column value, and hence, I need to turn it ON to run my scripts.

It all comes together rather nicely:

  • <!--- Update the data records. --->
  • <cfquery name="qTurnOnInsert" datasource="#DATA.Source#" username="#DATA.Username#" password="#DATA.Password#">
  • <!--- Delete the data in the table. --->
  • DELETE FROM
  • [#strTableName#]
  • ;
  •  
  • <!--- Turn on the ability insert the identity column. --->
  • SET IDENTITY_INSERT [#strTableName#] ON
  • ;
  • </cfquery>
  •  
  • <!--- Loop over the data query to add new rows. --->
  • <cfloop query="qData">
  • <!--- Inser this row. --->
  • <cfquery name="qInsert" datasource="#DATA.Source#" username="#DATA.Username#" password="#DATA.Password#">
  • INSERT INTO [#strTableName#]
  • (
  • <cfloop index="intColumn" from="1" to="#ListLen(qData.ColumnList)#" step="1">
  • <cfif (intColumn GT 1)>,</cfif>
  • [#ListGetAt( qData.ColumnList, intColumn )#]
  • </cfloop>
  • ) VALUES (
  • <cfloop index="intColumn" from="1" to="#ListLen(qData.ColumnList)#" step="1">
  • <cfif (intColumn GT 1)>,</cfif>
  • <cfqueryparam value="#qData['#ListGetAt( qData.ColumnList, intColumn )#'][qData.CurrentRow]#" cfsqltype="CF_SQL_VARCHAR" />
  • </cfloop>
  • )
  • ;
  • </cfquery>
  • </cfloop>
  •  
  • <!--- Turn off the ability insert the identity column. --->
  • <cfquery name="qTurnOffInsert" datasource="#DATA.Source#" username="#DATA.Username#" password="#DATA.Password#">
  • SET IDENTITY_INSERT [#strTableName#] OFF;
  • </cfquery>

 



Reader Comments

Mar 27, 2007 at 2:29 PM // reply »
1 Comments

MS-SQL DTS would work a lot better for data transfer. You can carry the indexes and other objects if necessary. Also alows pulling data by query and from just about any data source.


May 19, 2010 at 4:57 PM // reply »
1 Comments

Thanks for the code, this makes my job of inserting Identity values into another table that much easier.
Best,
Mike D


May 19, 2010 at 4:59 PM // reply »
10,638 Comments

@Mike,

My pleasure.


Jul 23, 2010 at 11:23 AM // reply »
2 Comments

"LET and SET statements are no longer supported" in ASP.NET


Jul 23, 2010 at 11:25 AM // reply »
10,638 Comments

@Jim,

What do you mean in ASP.NET? The "SET" command here is part of SQL. Are you referring to a specific database?


Jul 23, 2010 at 11:29 AM // reply »
2 Comments

Sorry, it works perfectly well, I just didn't execute it as a SQL statement.

cmd1.commandtext = "SET IDENTITY_INSERT dua ON"
cmd1.ExecuteNonQuery()


Jul 23, 2010 at 11:30 AM // reply »
10,638 Comments

@Jim,

Ah, no worries :) Glad you got it working now.


Oct 19, 2010 at 2:31 PM // reply »
2 Comments

I'm still getting an insert error when I have turned the identity on.

(SET IDENTITY_INSERT dao.Applications ON)-> Command(s) completed successfully.

Msg 544, Level 16, State 1, Line 1
Cannot insert explicit value for identity column in table 'Applications' when IDENTITY_INSERT is set to OFF.


Oct 19, 2010 at 2:51 PM // reply »
2 Comments

I added "GO" before and after my insert stmnts and it worked! Thanks!


Oct 20, 2010 at 9:57 AM // reply »
10,638 Comments

@Flip,

Interesting. What database system are you using?


Nov 8, 2010 at 8:59 AM // reply »
1 Comments

I have a remark (maybe trivial, maybe not) - I tried executing this via Management Studio and after this doing insert in another query editor and it kept failing. But when I pasted both into one window and executed in a single click then it worked fine - seems like the setting is rather temporary.


Dec 9, 2010 at 1:18 PM // reply »
1 Comments

How do I deal with turning Identity_Insert On with ORM? All I'm doing is creating a new entity and then calling the entitySave() method, and I get the message: "Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF."

Maybe I could insert a cfquery and explicitly turn it on before calling the entitySave method, but wouldn't that defeat the purpose of the ORM framework?


Jan 11, 2011 at 1:59 PM // reply »
1 Comments

I just keep getting
"Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF."

I am using SQL Server 2008 and executing the the script from SQL Server Management Studio


Feb 7, 2011 at 9:48 PM // reply »
1 Comments

rad!


Jun 13, 2011 at 12:47 AM // reply »
1 Comments

Thanks dude, i am confused between
(SET IDENTITY _INSERT tablename ON) or (SET IDENTITY_INSERT tablename ON) thanks.....


Oct 17, 2011 at 1:37 AM // reply »
1 Comments

how to call database function in SeaSar2



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 3, 2012 at 10:49 PM
How I Got Node.js Running On A Linux Micro Instance Using Amazon EC2
Wow this was really helpful! Only thing I would add is you need to update your .bash_profile after you edit the secure_path. This is what I did: $ . ~/.bash_profile Otherwise, NPM won't be found. ... read »
Feb 3, 2012 at 10:14 PM
Pushing Base64-Encoded Images Over HTML5 WebSockets With Pusher And ColdFusion
@Ben, Just wanted to let you know that pusher are soon to start limiting sizes on messages. This was the detail that came through in the Feb dispatch: "However, we will soon be limiting the s ... read »
Feb 3, 2012 at 5:05 PM
Regular Expressions Make CSV Parsing In ColdFusion So Much Easier (And Faster)
I tried using your RegEx in my C# program, but it was matching an extra empty-string at the end and so I would end up with an extra field that doesn't exist, so I changed it to this: (^|,)("(?: ... read »
Feb 3, 2012 at 3:47 PM
ColdFusion Supports HTTP Verbs PUT And DELETE (As Well As GET And POST)
Josh Cyr posted this on Twitter just a little bit ago. Thought it was appropriate. http://stackoverflow.com/questions/1619152/how-to-create-rest-urls-without-verbs/1619677#1619677 ... read »
Feb 3, 2012 at 2:28 PM
Changing The Execution Context Of Your Self-Executing Function Blocks In JavaScript
@Michael, You definitely make a good point (and extra points for quoting movies - I love movies). When you use a return() statement to define the object's public API, it does provide a consistent a ... read »
Feb 3, 2012 at 2:04 PM
Changing The Execution Context Of Your Self-Executing Function Blocks In JavaScript
To quote Jurassic Park: "Just because you can doesn't mean you should". I completely, utterly disagree with the thought that this is more readable. Consider the current module pattern: if ... read »
Feb 3, 2012 at 1:10 PM
REST API Design Rulebook By Mark Masse
@Jordan, Yeah, WRML was created by Mark Masse (author of the book). I also found it to be a bit convoluted. I suppose it is intended to allow the Client to be able to programmaticaly respond to cha ... read »
Feb 3, 2012 at 1:08 PM
ColdFusion Supports HTTP Verbs PUT And DELETE (As Well As GET And POST)
@Jason, To be honest, I don't have good answers for that kinds of stuff. And, to the point, that is specifically why I *really* liked the REST API Design Rulebook by Mark Masse - he just cuts throu ... read »