Turning Off and On Identity Column in SQL Server

Posted April 21, 2006 at 2:05 PM

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:

 Launch code in new window » Download code as text file »

  • <!--- 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>

 

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page




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.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 20, 2010 at 12:07 PM
Drawing On The iPhone Canvas With jQuery And ColdFusion
Simply awesome. Saved my day. ... read »
Mar 20, 2010 at 9:00 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
I would like to say thx for an easy way to create a bottom bar. I do have a ?. Is it possible to center the bar if i want to resize it to ex 85%. Regards Offenbach ... read »
Mar 19, 2010 at 7:26 PM
MySQL 3/4 - com.mysql.jdbc.Driver And allowMultiQueries=true
Thank you very much for this post. Adding allowMultiQueries="true" in context.xml didn't help until I added it to url as allowMultiQueries=true Good idea is to use prepared statements and it will he ... read »
Jim
Mar 19, 2010 at 4:49 PM
Nobody Puts Baby In The Corner!
Wow. This is like suddenly finding a support group for your secret shame. I'm not alone! I always liked this movie, even though it is extremely cheesy. I just wish Jennifer Grey hadn't gotten the ... read »
Mar 19, 2010 at 4:47 PM
Application.cfc OnRequest() Method Affects OnError() Arguments
@Jason and @Ben, I've been doing some CF9 refactoring on our systems and noticed an odd occurrence with onError as well. Found a way to work around my problem, but what I saw was... Background: Our ... read »
Jim
Mar 19, 2010 at 4:44 PM
Shoot 'Em Up Starring Clive Owen And Paul Giamatti
I actually enjoyed this movie quite a lot. It was different, certainly, but I think they were going for more of a Quentin Tarentino-"wow, that was weird"-vibe than an actual spoof. Once I realize ... read »
Mar 19, 2010 at 4:34 PM
An Intensive Exploration Of jQuery With Ben Nadel (Video Presentation)
Hey I guess the video is down. Is there anyway you can upload to youtube or vimeo or some other service? Greatly appreciated. ... read »
Mar 19, 2010 at 4:24 PM
ColdFusion CFPOP - My First Look
@Ben Thanks for the follow up! The root of the problem had to do with being able to trace bounced emails to specific records in a DB table. Let's say you run an email campaign and you get 1,000 bou ... read »