Created
March 24, 2014 22:54
-
-
Save bennadel/9751088 to your computer and use it in GitHub Desktop.
Turning Off and On Identity Column in SQL Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- 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