[英]Can INSERT and DELETE be added in one single executeBatch
I have hundreds of thousand of lines of text which need to be inserted and amang which there are hundred lines which should be deleted. 我有数十万行文本需要插入,而amang有几百行应删除。 The text file looks like below: 文本文件如下所示:
i 01 ppp
i 02 vvv
i 45 bbb
...
d 05
i 09 mmm
i 21 jjj
....
"i" indecates INSERT, "d" indecates deletes DELETE, the rest text of a lne is data which need be handled. “ i”表示插入,“ d”表示删除DELETE,线的其余文本是需要处理的数据。
insert into t1 (id, name)values(?, ?);
delete from t1 where id = ?;
once a time I read 1000 lines , addBatch(), and executeBatch() for these "i" head lines, and those "d" head lines handled at last. 一次,我读取了这些“ i”标题行和最后处理的“ d”标题行的1000行,addBatch()和executeBatch()。 Is it possible that i add mixed insert and delete batch, then execute them? 我是否可以添加混合的插入并删除批处理,然后执行它们?
thanks. 谢谢。
You need 2 multi-row statements. 您需要2个多行语句。
INSERT INTO t1 (id, name) VALUES (?, ?), (?, ?) ...;
DELETE FROM t1 WHERE id IN (?, ?, ...);
If you want atomic operation, lock table (statement depends on you DB) 如果要原子操作,请锁定表(语句取决于您的数据库)
Depending on the database in question, you could try use the merge
statement 根据所讨论的数据库,您可以尝试使用merge
语句
Take a look at Oracle's Merge statement for example 以Oracle的 Merge语句为例
MERGE INTO bonuses D
USING (SELECT employee_id, salary, department_id FROM employees
WHERE department_id = 80) S
ON (D.employee_id = S.employee_id)
WHEN MATCHED THEN UPDATE SET D.bonus = D.bonus + S.salary*.01
DELETE WHERE (S.salary > 8000)
WHEN NOT MATCHED THEN INSERT (D.employee_id, D.bonus)
VALUES (S.employee_id, S.salary*0.1)
WHERE (S.salary <= 8000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.