自动增量可编辑列

时间:2019-05-22 10:34:35

标签: sql sql-server database-migration auto-increment identity-column

我正在寻找遇到的问题的最佳解决方案。

我有几个数据库,我经常将它们对齐以在其中包含相同的数据。在某些引用表中,有“自动递增ID”列,在其他表中,有与这些ID值匹配的列。不幸的是,在源DB上的ID更改后可能像是(10,15,20),而在将其插入到目标表后,它是(1,2,3)。我希望我的模型可以澄清这种情况:

源数据库引用表:

ID_________Name
10_________a
15_________b
20_________c

目标数据库引用表:

ID_________Name
1__________a
2__________b
3__________c

来源情况表:

RefTableID___OtherColumns
10__________a
10__________b
15__________c
15__________d

问题在于,将FactTable迁移到目标数据库后,它与RefTable中的记录不匹配。我有一个想法要添加另一列,该列与ID列相比将是可编辑的。问题是最终用户不应该看到它(这对他没有任何意义),因此它也应该是自动递增的。在这种情况下我应该使用什么:

  • 顺序?
  • 触发?
  • -添加/保存存储过程?

或者也许有人对如何解决这个问题有更好的主意?

1 个答案:

答案 0 :(得分:0)

在SQL Server中,一旦将行插入表中就无法更新标识列。

但是,可以通过为指定表使用set identity_insert手动插入它的值。 在每个会话中,您只能有一个表,同时将<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="my:my"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <my:map> <map> <item from="13days" to="1week" /> <item from="14days" to="2week" /> <item from="27days" to="3week" /> </map> </my:map> <xsl:variable name="vMap" select="document('')/*/my:map/*/*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="@maturity[. = document('')/*/my:map/*/*/@from]"> <xsl:attribute name="maturity"> <xsl:value-of select="$vMap[@from = current()]/@to"/> </xsl:attribute> </xsl:template> </xsl:stylesheet> 设置为identity_insert,因此,插入完成后,请务必将其重新设置为on

代码可能看起来像这样:

off
相关问题