当我尝试使用 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 处理这个问题?