最好的方法做双插入 [英] Best way to make double insert

查看:192
本文介绍了最好的方法做双插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在表A中插入信息以及使用表A中的索引与表B相关联的最佳方式是什么。

What's the best way of inserting information in table A and using the index from table A to relate to table B.

我尝试的解决方案信息在表A中(其具有自动生成的ID),然后选择最后一个索引并将其插入到表B中。这可能不是非常有用,因为最后的索引可以在插入之间改变,因为另一个用户可以生成新索引在表A中

The "solution" I tried is inserting the info in table A (which has a automatically generated ID), then, select the last index and insert it in table B. This may not be very useful, as the last index may change between the inserts because another user could generate a new index in table A

我遇到过这个问题与各种DBMS postgreSQL,Informix,MySQL和MSSQL(感谢lomaxx的答案)

I have had this problem with various DBMS postgreSQL, Informix, MySQL and MSSQL (thanks to lomaxx for the answer)

推荐答案

如果你使用MSSQL,你可以使用SCOPE_IDENTITY返回当前会话中插入的最后一个id。然后可以使用它插入表B中。

if you're using MSSQL you could use SCOPE_IDENTITY to return the last id inserted in your current session. You can then use that to insert into table B.

本文从MSDN 给出了一个如何做的体面例子。

This article from MSDN gives a decent example on how to do it.

这篇关于最好的方法做双插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆