简体   繁体   English

INSERT INTO 使用外键和另一个表中的数据

[英]INSERT INTO with foreign key and data from another table

I have a table where the most important information is that it is auto-incremented, the rest of the fields in the database are not relevant.我有一个表,其中最重要的信息是它是自动递增的,数据库中的其余字段不相关。 Before inserting the data into the table, I created a "helper" table to store the newly created IDs in this table.在将数据插入到表中之前,我创建了一个“帮助”表来将新创建的 ID 存储在该表中。

I have a second table like this - also the most important information is that the ID is auto-incremented, and the other data is not relevant to this example.我有第二个这样的表 - 最重要的信息是 ID 是自动递增的,其他数据与此示例无关。 In this case, I have also created an auxiliary table that stores the newly created ID values from this table.在这种情况下,我还创建了一个辅助表,用于存储该表中新创建的 ID 值。

Now I would like to take the values from auxiliary table 1 and 2 and insert them into a third table that will take the smallest ID from auxiliary table 1 and the smallest ID from auxiliary table 2 and insert them as a record into this third table, for example: Record ID of third table |现在我想从辅助表 1 和 2 中获取值并将它们插入到第三个表中,该表将从辅助表 1 中获取最小 ID,从辅助表 2 中获取最小 ID,并将它们作为记录插入到第三个表中,例如:第三张表的记录ID | Smallest ID from first table |第一个表中的最小 ID | Smallest ID from third table.第三个表中的最小 ID。

I have no idea how to build the query constructs in my case - could someone give me some advice, or ready-made (different) code to follow?我不知道如何在我的情况下构建查询结构 - 有人可以给我一些建议,或者可以遵循的现成(不同)代码吗?

My code:我的代码:

DECLARE @inserted1 TABLE (contact_id udt_id)
INSERT INTO t_usr_contact (contact_firstname, contact_lastname) 
OUTPUT INSERTED.contact_id INTO @inserted1(contact_id)
SELECT
'Firma',
'Temporary_value'
FROM t_sup_supplier AS sup
WHERE sup.sup_id IN (175,176) AND sup.grp_id IS null 

DECLARE @inserted2 TABLE (grp_id udt_id)
INSERT INTO t_usr_group (grp_label_en)
OUTPUT INSERTED.grp_id INTO @inserted2(grp_id)
SELECT
'Supplier contact'
FROM t_sup_supplier AS sup2
WHERE sup2.sup_id IN (175,176) AND sup2.grp_id IS null

INSERT INTO t_usr_contact_group (grp_id, contact_id)

I would like to go the easiest way, which is as below, but it doesnt work :/.我想采用最简单的方法,如下所示,但它不起作用:/。

VALUES (@inserted2.grp_id, @inserted2.contact_id)


As for the data example, after the insert in the first table I will get the following records and in the auxiliary table number 1 I will get the following records:至于数据示例,在第一个表中插入后,我将获得以下记录,在辅助表编号 1 中,我将获得以下记录:

**Table t_usr_contact:**
175 - Firma - Temporary_value
176 - Firma - Temporary_value
**Table @inserted1:**
175
176
**Table t_usr_group:**
201 - Supplier_contact
202 - Supplier_contact
**Table @inserted2:**
201
202
**Table t_usr_contact_group:**
201 - 175
202 - 176

I've got no idea what you're ultimately trying to do, but if you want two tables each with N rows to become one table made from the columns of the two input tables, like you've got in your example (where your table of 175,176 and your table of 201,202 shall become a table of 175|201,176|202) then you need to join them.我不知道你最终想要做什么,但是如果你想让两个表分别有 N 行成为一个由两个输入表的列组成的表,就像你在你的例子中那样(你的175,176 的表和 201,202 的表将变为 175|201,176|202 的表)然后你需要加入它们。 To join them you need a key.要加入他们,您需要一把钥匙。 You haven't got a key so you'll have to fake one:你没有钥匙,所以你必须伪造一个:

INSERT INTO thirdtable
SELECT contact_id,grp_id
FROM 
  (SELECT *, ROW_NUMBER() OVER(ORDER BY contact_id) as FakeKey FROM @inserted1) x
  INNER JOIN
  (SELECT *, ROW_NUMBER() OVER(ORDER BY grp_id) as FakeKey FROM @inserted2) x
  ON x.FakeKey = y.FakeKey

This, of course, joins the data in a very arbitrary fashion based on the order of the assigned IDs.当然,这会根据分配的 ID 的顺序以非常任意的方式连接数据。 If you want some specific order, like contact 175 exists first and has to get group 202, then you can make the query that inserts the group (eg 202) based on the input 175 output the 175 and the 202 together into a (temp) common table then split it into the detail and middleman tables after如果您想要一些特定的顺序,例如联系人 175 首先存在并且必须获取组 202,那么您可以进行查询,根据输入 175 插入组(例如 202),将 175 和 202 一起输出到(临时)公用表,然后将其拆分为明细表和中间人表

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

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