【发布时间】:2014-11-08 09:33:06
【问题描述】:
我正在尝试将一些数据插入到我的 ms sql 数据库 (ms sql server 2012) 中的 2 个不同的表中。
请考虑以下 2 个表格,其中包含有关民意调查及其选择的信息。
+----------------+
| Polls |
+----------------+
| pollID | The id of a poll (auto increment)
| memberID | The id of the member, owning the poll
| pollTitle | The title/question of the poll
| date | The date of the poll
+----------------+
+----------------+
| PollChoices |
+----------------+
| pollChoiceID | The id of a poll choice (auto increment)
| pollID | The id of the poll, that include this choice
| pollChoice | The name/title of the poll choice
+----------------+
如何进行查询,以最有效的方式插入数据? 我总是可以进行 2 个查询,但无法真正弄清楚如何用一个查询来完成。
在这种情况下,对我来说主要问题之一是在插入民意调查时获取民意调查的 id。我怎样才能获得这个新插入的“pollID”(自动增量)并在同一个查询中使用它?
另外,是否有必要使用事务或存储过程(我在某处读过这个)?
任何帮助将不胜感激。
【问题讨论】:
标签: sql-server join sql-insert