Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 24, 2014 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennadel/9751088 to your computer and use it in GitHub Desktop.
Save bennadel/9751088 to your computer and use it in GitHub Desktop.
Turning Off and On Identity Column in SQL Server
<!--- 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment