如何使用具有多个值的JOOQ的insertInto? (Sybase数据库)

时间:2019-05-24 12:35:44

标签: sybase jooq

当我尝试使用JOOQ将多个值插入Sybase数据库时出现异常:

Java代码:

dsl.insertInto(ACTIVITY).columns(ACTIVITY.ACTIVITY_NO, ACTIVITY.SUMMARY)
    .values(1, "summary 1")
    .values(2, "summary 2")
    .execute()

已变成SQL:

org.jooq.tools.LoggerListener - Executing query          : insert into [activity] ([activity_NO], [summary]) select * from (select ?, ?) x union all select * from (select ?, ?) x
org.jooq.tools.LoggerListener - -> with bind values      : insert into [activity] ([activity_NO], [summary]) select * from (select 1, 'summary 1') x union all select * from (select 2, 'summary 2') x

insert into ACTIVITY (ACTIVITY_NO, SUMMARY)
select *
from (select 1, 'summary 1') x
union all
select *
from (select 2, 'summary 2') x

导致异常:

org.jooq.exception.DataAccessException: SQL [insert into [activity] ([activity_NO], [summary]) select * from (select ?, ?) x union all select * from (select ?, ?) x]; A derived table expression may not have null column names. Use a derived column list in the derived table definition or name the column expressions in the SELECT target list.

该异常来自Sybase数据库:http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc32300.1550/html/sqlug/sqlug423.htm

正确的SQL将是:

insert into ACTIVITY (ACTIVITY_NO, SUMMARY)
select *
from (select 1 as ACTIVITY_NO, 'summary 1' as SUMMARY) x
union all
select *
from (select 2 as ACTIVITY_NO, 'summary 2' as SUMMARY) x

如何使用JOOQ处理此问题?

0 个答案:

没有答案