mybatis insert all nextval unique constraint error in #transaction mdoe - spring

MyBatis 3 spring - java
I am trying to batch insert and and getting following error when there are more then 1 records. it works perfect with one recrod
I beleive b/c it is in transaction nextval is not generating nextval on each iteration. am i correct any help on that ?
nested exception is java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (LINEAR_UPSELL.SYS_C0016697) violated
and my method has Transaction annotation in java file
#Transactional(propagation=Propagation.REQUIRED, readOnly=false, rollbackFor=Exception.class)
in mapper file following is my insert statement
<insert id="insertService" parameterType="java.util.List">
insert all
<foreach collection="list" item="ch" index="index" >
into tva_upselladmin_channel (id, source_id, service_id, name) values (
TVA_UPSELLADMIN_CHANNEL_SEQ.nextVal,
#{ch.sourceId},
#{ch.serviceId},
#{ch.name}
)
</foreach>
SELECT * FROM dual
</insert>

in oracle nextval does not work very well with insert all statement. you have to find work around as following
extractvalue(dbms_xmlgen.getxmltype('select TVA_UPSELLADMIN_CHANNEL_SEQ.nextval - 1 from dual'),'//text()')
complete insert as following have no idea with -1 is there.
<insert id="insertServiceMappings" parameterType="java.util.List">
insert all
<foreach collection="list" item="channel" index="index" >
into tva_upselladmin_channel (id, source_id, service_id, name) values (
extractvalue(dbms_xmlgen.getxmltype('select TVA_UPSELLADMIN_CHANNEL_SEQ.nextval - 1 from dual'),'//text()'),
#{channel.sourceId},
#{channel.serviceId},
#{channel.name}
)
</foreach>
SELECT * FROM dual
</insert>

Related

FRM-40735: post-insert trigger raised unhandled exception ora-01722

I create POST-INSERT Trigger on Block when I am getting this error on Oracle Forms 11gR2
"FRM-40735: post-insert trigger raised unhandled exception ora-01722"
POST-INSERT Trigger Code:
Insert into we_group (GROUP_ID, GROUP_SIZE, NRSP_STATUS, GROUP_RECEIVED)
Select DISTINCT GROUP_ID, ('Select COUNT(*) from we_group_hof_k'),
nrsp_status, sysdate
from we_group_hof_k;
commit_form;
How to solve this problem?
Remove ' to prevent number to char convertion
Select DISTINCT GROUP_ID, (select COUNT(*) from we_group_hof_k),
nrsp_status, sysdate
from we_group_hof_k;
Consider using analytic version of the COUNT function (if Forms version you use supports it; 10g doesn't, I can't tell about 11g):
INSERT INTO we_group (GROUP_ID,
group_size,
nrsp_status,
group_received)
SELECT DISTINCT GROUP_ID,
COUNT (*) OVER (ORDER BY NULL),
nrsp_status,
SYSDATE
FROM we_group_hof_k;
It evidently seems ORA-01722 raises due to an attempt to insert a quoted string
( 'Select COUNT(*) from we_group_hof_k' ) into a numeric column( GROUP_SIZE ).
So, first of all you need to get rid of those quotes, and even whole sub-select, since you already try to select from the same table in the main query, and just consider to include a group by clause instead:
Insert Into we_group(group_id, group_size, nrsp_status, group_received)
Select group_id,Count(1),nrsp_status, sysdate
From we_group_hof_k
Group By group_id,nrsp_status;
Lastly do not use a commit or commit_form inside a POST-INSERT trigger, that usage is considered as illegal restricted there.

mybatis batch insert generating duplicate uuid primary key

when i insert single record:
<insert id="add" parameterType="SysUser">
<selectKey keyProperty="id" resultType="String" order="BEFORE">
select replace(uuid(),'-','') from dual
</selectKey>
insert into sys_user(id,user_name,user_email,user_info,user_password,create_time)
values
(#{id,jdbcType=VARCHAR},
#{userName,jdbcType=VARCHAR},
#{userEmail,jdbcType=VARCHAR},
#{userInfo,jdbcType=VARCHAR},
#{userPassword,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP})
</insert>
but i want to insert a list, how can i generate the uuid like the single insert above???
<insert id="addSysUsers" parameterType="List" useGeneratedKeys="true" keyProperty="id">
INSERT INTO sys_user(user_name,user_password,user_email)
VALUES
<foreach collection="sysUsers" item="sysUser" separator=",">
(#{sysUser.userName,jdbcType=VARCHAR},#{sysUser.userPassword,jdbcType=VARCHAR},#{sysUser.userEmail,jdbcType=VARCHAR})
</foreach>
</insert>
The simplest way to do it is put the UUID generator in the values block:
<insert id="add" parameterType="SysUser">
insert into sys_user(id,user_name,user_email,user_info,user_password,create_time)
values
(replace(uuid(),'-',''), <!-- create uuid directly here --->
#{userName,jdbcType=VARCHAR},
#{userEmail,jdbcType=VARCHAR},
#{userInfo,jdbcType=VARCHAR},
#{userPassword,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP})
</insert>

Getting ORA-00904 invalid identifier executing multi table insert query

I'm just trying 'multi table insert'. Below is my insert query. I'm trying to insert values from employees table to table t1,t2 and t3. After executing the query i'm getting an error.
ERROR at line 4:
ORA-00904: "EMPLOYEES"."LAST_NAME": invalid identifier
The column last_name is exist in employees table. But why i'm getting this error.
insert all
into t1(id,l_name) values(employees.employee_id,employees.last_name)
into t2(id,l_name) values(employees.employee_id,employees.last_name)
into t3(id,l_name) values(employees.employee_id,employees.last_name)
select * from employees;
/
I've also tried replacing table name and column name to upper case. Still facing the same error. I'm using Oracle 10g.
Thanks
After removing the reference table name employees from column name its working.
Answer:
INSERT ALL
INTO t1(id, l_name) VALUES (employee_id, last_name)
INTO t2(id, l_name) VALUES (employee_id, last_name)
INTO t3(id, l_name) VALUES (employee_id, last_name)
SELECT * FROM employees;

insert multiple row into table using select however table has primery key in oracle SQL [duplicate]

This question already has answers here:
How to create id with AUTO_INCREMENT on Oracle?
(18 answers)
Closed 8 years ago.
I am facing issue while inserting multiple row in one go into table because column id has primary key and its created based on sequence.
for ex:
create table test (
iD number primary key,
name varchar2(10)
);
insert into test values (123, 'xxx');
insert into test values (124, 'yyy');
insert into test values (125, 'xxx');
insert into test values (126, 'xxx');
The following statement creates a constraint violoation error:
insert into test
(
select (SELECT MAX (id) + 1 FROM test) as id,
name from test
where name='xxx'
);
This query should insert 3 rows in table test (having name=xxx).
You're saying that your query inserts rows with primary key ID based on a sequence. Yet, in your insert/select there is select (SELECT MAX (id) + 1 FROM test) as id, which clearly is not based on sequence. It may be the case that you are not using the term "sequence" in the usual, Oracle way.
Anyway, there are two options for you ...
Create a sequence, e.g. seq_test_id with the starting value of select max(id) from test and use it (i.e. seq_test_id.nextval) in your query instead of the select max(id)+1 from test.
Fix the actual subselect to nvl((select max(id) from test),0)+rownum instead of (select max(id)+1 from test).
Please note, however, that the option 2 (as well as your original solution) will cause you huge troubles whenever your code runs in multiple concurrent database sessions. So, option 1 is strongly recommended.
Use
insert into test (
select (SELECT MAX (id) FROM test) + rownum as id,
name from test
where name='xxx'
);
as a workaround.
Of course, you should be using sequences for integer-primary keys.
If you want to insert an ID/Primary Key value generated by a sequence you should use the sequence instead of selecting the max(ID)+1.
Usually this is done using a trigger on your table wich is executed for each row. See sample below:
CREATE TABLE "MY_TABLE"
(
"MY_ID" NUMBER(10,0) CONSTRAINT PK_MY_TABLE PRIMARY KEY ,
"MY_COLUMN" VARCHAR2(100)
);
/
CREATE SEQUENCE "S_MY_TABLE"
MINVALUE 1 MAXVALUE 999999999999999999999999999
INCREMENT BY 1 START WITH 10 NOCACHE ORDER NOCYCLE NOPARTITION ;
/
CREATE OR REPLACE TRIGGER "T_MY_TABLE"
BEFORE INSERT
ON
MY_TABLE
REFERENCING OLD AS OLDEST NEW AS NEWEST
FOR EACH ROW
WHEN (NEWEST.MY_ID IS NULL)
DECLARE
IDNOW NUMBER;
BEGIN
SELECT S_MY_TABLE.NEXTVAL INTO IDNOW FROM DUAL;
:NEWEST.MY_ID := IDNOW;
END;
/
ALTER TRIGGER "T_MY_TABLE" ENABLE;
/
insert into MY_TABLE (MY_COLUMN) values ('DATA1');
insert into MY_TABLE (MY_COLUMN) values ('DATA2');
insert into MY_TABLE (MY_ID, MY_COLUMN) values (S_MY_TABLE.NEXTVAL, 'DATA3');
insert into MY_TABLE (MY_ID, MY_COLUMN) values (S_MY_TABLE.NEXTVAL, 'DATA4');
insert into MY_TABLE (MY_COLUMN) values ('DATA5');
/
select * from MY_TABLE;

Insert Overwrite for multiple inserts in hive which have the same partition with same parameter value

Hi Guys,
So I am trying to do multiple inserts and i am successfully able to
do it but if there are two queries which have same partition and
static value assigned it gives me the following error
:15:02:22 [EXPLAIN - 0 row(s), 0.000 secs] [Error Code: 10087, SQL State:
42000] Error while compiling statement: FAILED: SemanticException
[Error 10087]: The same output cannot be present multiple times:
table_name#id=0
here first insert happens successfully but because second insert has the same value assigned for id
which is 0 ..it gives the above error ..please let me know a
workaround.Thanks :)
FROM (
Select * from Table_Name
)Query
INSERT OVERWRITE TABLE Table_Name PARTITION(id=0)
select column1,column2,column3
GROUP BY column1,column2,column1
INSERT OVERWRITE TABLE Table_Name PARTITION(id=0)
select column1,count(*) as column2
Instead of multiple inserts, you can just do one insert with a union of the two queries.
FROM (
Select * from Table_Name
)Query
INSERT OVERWRITE TABLE Table_Name PARTITION(id=0)
select <query 1>
UNION ALL
select <query 2>

Resources