zoukankan      html  css  js  c++  java
  • Turning Off and On Identity Column in SQL Server

    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>
  • 相关阅读:
    使用qemu模拟调试内核和debian根文件系统
    qemu无界面启动,并重定向输出到终端
    linux 音频编程
    解决Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题
    【BigData】Java基础_通用排序工具类的实现
    【BigData】Java基础_登录注册实现
    【费曼学习方法】世界上最好的学习方法
    【BigData】Java基础_接口
    【BigData】Java基础_Eclipse配置自动补全new
    【BigData】Java基础_HashSet
  • 原文地址:https://www.cnblogs.com/weaver1/p/2381787.html
Copyright © 2011-2022 走看看