简体   繁体   English

从表2到表3的SQL INSERT SELECT

[英]SQL INSERT SELECT from Table2 to Table3

I'm trying to insert ID from Table1 and Table2 into Table3.My table design is: 我正在尝试将Table1和Table2的ID插入Table3。我的表设计是:

Table1:
Id_Table1
field1 etc.

Table2:
Id_Table2
field1 etc.

Table3:
Id_Table3
Id_Table1_FK
Id_Table2_FK

I do this when I save records for Table1. 当我保存表1的记录时,我将执行此操作。 Meanwhile I have Datagridview in my form, which uses comboboxcell to view records from Table2. 同时,我的表单中有Datagridview,它使用comboboxcell来查看Table2中的记录。 When I select some record from Table2, both ID's (Table1 & Table2) needs to be inserted into Table3. 当我从表2中选择一些记录时,两个ID(表1和表2)都需要插入到表3中。

Here is my code so far (obviously wrong): 到目前为止,这是我的代码(显然是错误的):

INSERT INTO Table3 (Id_Table3, Id_Table1, Id_Table2) 

SELECT Id_Table2 FROM Table2 WHERE serial_no=" & MyDGV.CurrentRow.Cells(0).Value.ToString

VALUES (Id_Table3_seq.nextval, Id_Table1_seq.currval,????)

So, my problem stucks with Id_Table2 which needs to be selected first by Datagridview.Row cell value, and then result passed like a parameter to Table3. 因此,我的问题卡在Id_Table2上,它需要首先由Datagridview.Row单元格值选择,然后将结果像参数一样传递给Table3。 Any suggestions ? 有什么建议么 ? Probably an easy one, I just don't know how to start... 可能很简单,我只是不知道该如何开始...

If I got your point right, this is what you need maybe? 如果我的观点正确,也许这就是您所需要的?

INSERT INTO Table3 (Id_Table3, Id_Table1, Id_Table2) 
VALUES (Id_Table3_seq.nextval, Id_Table1_seq.currval,(
SELECT Top 1 Id_Table2 FROM Table2 WHERE serial_no='" & MyDGV.CurrentRow.Cells(0).Value.ToString & "'
))

Edited: Added Top 1 to make sure the subquery will only return 1 recordset. 编辑:添加了Top 1 ,以确保子查询将只返回1个记录集。 It's optional if the field you are selecting is unique. 如果您选择的字段是唯一的,则是可选的。

Use a SELECT that returns constant valus: 使用SELECT返回常量值:

INSERT INTO Table3 (Id_Table3, Id_Table1, Id_Table2) 
select Id_Table3_seq.nextval, 
       Id_Table1_seq.currval,
       Id_Table2
FROM Table2 
WHERE serial_no='" & MyDGV.CurrentRow.Cells(0).Value.ToString & "';

In general you should not concatenate values into a SQL string. 通常,您不应将值连接到SQL字符串中。 Use a prepared statement with parameters (placeholders) instead (don't know how that is done in VB.net) 使用带有参数(占位符)的预备语句(不知道在VB.net中是如何完成的)

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

相关问题 插入table1从table2,table3中选择,其中 - insert table1 select from table2,table3 where 从table1插入一个table2获取id然后插入table3 SQL服务器 - Insert into one table2 from table1 get the id and then insert into table3 SQL server sql server将数据从table1插入table2,其中table1 = table3数据 - sql server insert data from table1 to table2 where table1=table3 data 从一个表1中选择多个值,在表2中查找并在表3中插入 - Select multiple values from one table1, find in table2 and insert in table3 如何“插入表(col1,col2)值(从table2中选择max(id),从table3中选择id); “? - how to “insert into table (col1, col2) values (select max(id) from table2, select id from table3); ”? PostgreSQL:将table1复制到table2和table3,但使用返回的table2值插入table3 - postgresql: copy table1 to table2 and table3, but use returned table2 values to insert into table3 根据 table3 中的条件从 table2 插入 table1 - Insert into table1 from table2 based on conditions from table3 遍历table1,从table2中提取信息并插入到Teradata中的table3中 - Loop through table1, extract information from table2 and insert into table3 in Teradata SQL 获取 table1 的名称以及 table2 和 table3 的计数 - SQL get table1 names with a count of table2 and table3 从表1创建表3 AS SELECT WHERE ID =从表2创建ID - Create Table3 AS SELECT WHERE id from table1 = id from table2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM