I'm trying to write mapper using annotation mybatis for the following sql extract:
CREATE TEMP TABLE XYZ
.
/* Something */
.
CREATE TEMP TABLE ABC
as
Select ptype,pvalue,id,djob
from XYZ
where pvalue> 0
and CASE WHEN djob = ptype
then pvalue = d_job_partition_value END;
Insert into ABC
Select ptype,pvalue,id,djob
from XYZ
where
CASE WHEN ptype = 'region' AND d_job_partition_type = 'region'
THEN pvalue in (Select distinct CAST(region_id as NUMERIC(38,0)) from {db}.{table} where region_id=d_job_partition_value) END;
The mapper class that I tried to write keeping in mind that ABC
is a temporary table created in between.
@Select("with XYZ as
(
/* Something */
),
ABC as (
Select ptype,pvalue,id,djob
from XYZ
where pvalue> 0
and CASE WHEN djob = ptype
then pvalue = d_job_partition_value END),
insert into ABC
(ptype,pvalue,id,djob) values(
Select ptype,pavalue,id,djob
from XYZ
CASE WHEN ptype = 'region' AND d_job_partition_type = 'region'
THEN pvalue in (Select distinct CAST(region_id as NUMERIC(38,0)) from {db}.{table}
where region_id=d_job_partition_value) END ")
The error is:
ERROR: syntax error at or near "into"
[junit] Position: 5828
[junit] at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
[junit] at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:107)
[junit] at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:98)
[junit] at org.apache.ibatis.session.SqlSessionManager$SqlSessionInterceptor.invoke(SqlSessionManager.java:282)
[junit] at com.sun.proxy.$Proxy28.selectList(Unknown Source)
[junit] at org.apache.ibatis.session.SqlSessionManager.selectList(SqlSessionManager.java:171)
[junit] at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:114)
[junit] at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:58)
[junit] at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:43)
[junit] at com.sun.proxy.$Proxy29.okey(Unknown Source)
I'm aware that @Insert
is used to insert values . But in this case, creation of temp table is creating problem.
Thanks in advance.
You can specify multiple statements (including temp table creation) in @Insert
annotation on mapper method:
@Insert(
"create temp table xyz as select * from whatever; " +
"CREATE TEMP TABLE ABC as Select ptype,pvalue,id,djob from XYZ;" +
"Insert into ABC Select ptype,pvalue,id,djob from XYZ;"
)
void insertMultiple();
This solution works not only in postgres. I've check and it works with H2 as well.
Note that the option you are trying namely CTE aka with
-query does not create a temporary table.
They look like temporary table and even documentation says they can be thought of as defining temporary tables that exist just for one query
but this is only for explanatory purposes. They are not temporary tables and the important difference is that it is not possible to do insert into this auxiliary statements.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.