4

Can JOOQ do 'insert into select' syntax for specified columns? I run several different tries..

The Tables:

table1 (Expected insert)

id   column1   column2    column3 ...  timestamp
1    John      Leo        null         2012/1/28 23:32:23    (Expected insert)

table2

id   col1   col2   col3  
101  John   xxx    xxx     (from table2.col1)
102  xxx    xxx    xxx

table3


id   col1   col2   col3  
101  xxx    Leo    xxx     (from table3.col2)
102  xxx    xxx    xxx
INSERT INTO table1 ( column1, column2 )
SELECT  table2.col1, table3.col2
FROM table2 join table3 t3 on table2.id = table3.id 
where table2.id = 101;

JOOQ code:

create.insertInto(table1, column1, column2 )
      .values( create.select( table2.col1, table3.col2 )
                     .from(table2)
                     .join(table3)
                     .on( table12.id.equal(table3.id) )
                     .where( table2.id.equal(101) ))
     .execute(); //.getSQL();

JOOQ show error message:

The number of values must match the number of fields

Anyone know what problem I make and how I can fix my JOOQ code. thanks, Pay.

Reference: Example: INSERT SELECT syntax support

4

1 回答 1

8

您使用的是INSERT .. VALUES语法,而不是INSERT .. SELECT语法。您的子查询提供了值column1,但您没有为column2. 您要执行的操作在“示例:INSERT SELECT 语法支持”手册中进一步描述。在您的情况下,这将读取(jOOQ 2.x 语法,在 jOOQ 3.x 中不再可用):

create.insertInto(table1, 
create.select( table2.col1, table3.col2 )
      .from(table2)
      .join(table3)
      .on( table12.id.equal(table3.id) )
      .where( table2.id.equal(101) ))
      .execute(); //.getSQL();

或者使用自定义投影(jOOQ 3.x 语法):

create.insertInto(table1, column1, column2)
      .select(create.select(...))
      .execute();
于 2012-01-23T09:40:01.637 回答