简体   繁体   English

批处理插入返回标识和对象引用/序列

[英]Batch insert return identity and object reference/sequence

With SQL Server 2K8 from C# I'm trying to do a batch insert/updates of records to a parent/child tables to optimize. 使用C#的SQL Server 2K8,我试图对父/子表进行记录的批处理插入/更新,以进行优化。

The inserts/updates will generate a key automatically which I'd like to extract via an OUTPUT, etc. and then reassign back in the domain model. 插入/更新将自动生成一个密钥,我想通过OUTPUT等提取该密钥,然后将其重新分配给域模型。 For batch inserts I need to keep track of which newly generated ID belongs to which domain object in the batch list. 对于批处理插入,我需要跟踪批处理列表中哪个新生成的ID属于哪个域对象。

This example comes close to what I need, but was wondering if there's a way to not have an extra column added to the table (SequenceNumber) and still achieve the same results: http://illdata.com/blog/2010/01/13/sql-server-batch-inserts-of-parentchild-data-with-ibatis/ 这个例子很接近我的需要,但是想知道是否有一种方法可以不向表(SequenceNumber)中添加额外的列,而仍然达到相同的结果: http : //illdata.com/blog/2010/01/ 13 / SQL服务器分批插入-的-父子数据与- ibatis的/

ie. 即。 could we rely on the order of the inserts generated from the OUTPUT into the temp table, or pass a ref GUID set on the data model and passed temporarily to the SQL just for reference purposes? 我们可以依靠从OUTPUT生成的插入到temp表中的顺序,还是将数据模型上的ref GUID设置传递给SQL,并将其临时传递给SQL只是为了参考?

In SQL Server 2008 it is possible to use merge and output to get a mapping between the generated key and the key used in the staging table. 在SQL Server 2008中,可以使用合并输出来获取生成的密钥和登台表中使用的密钥之间的映射。

Have a look at this question. 看看这个问题。 Using merge..output to get mapping between source.id and target.id 使用merge..output获取source.id和target.id之间的映射

Unless I've misunderstood... 除非我误会了...

A surrogate key (IDENTITY or NEWID etc) isn't your actual object identifier. 替代键(IDENTITY或NEWID等)不是您实际的对象标识符。 It's an implementation detail and has no intrinsic meaning. 这是实现细节,没有内在含义。

You must have another identifier (name, ISBN, serial number, transaction code/date, etc) that is the real (natural) key. 您必须有另一个标识符(名称,ISBN,序列号,交易代码/日期等),它是真实(自然)密钥。

Your OUTPUT clause can return the surrogate key and the natural key. 您的OUTPUT子句可以返回代理键和自然键。 You then use this to map back 然后,您可以使用它来映射回来

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM