简体   繁体   English

可以在单个executeBatch中添加INSERT和DELETE

[英]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.

相关问题 如果一条记录无法插入,如何使Preparedstatement.executeBatch()对好的记录起作用 - How to get Preparedstatement.executeBatch() to work for the good records if one record fails to insert 我们如何在单个合并中进行更新,删除,插入 - How can we do update,delete,insert in single merge Oracle 在一条语句中删除和插入 - Oracle delete and insert in one statement 我可以使用 MERGE 语句通过一次操作在 2 个或多个表上使用插入、更新和删除子句吗? - Can I use MERGE statement to use insert, update and delete clauses on 2 or more tables through one operation? 为插入/更新/删除设置单个触发器还是多个触发器更好? - Is it better to have single trigger for Insert/Update/Delete, or multiple? 使用单个insert into语句一次性插入多个记录 - insert multiple records in one go using a single insert into statement 可以在oracle中同时进行插入和删除 - Can Insert and delete be done at the same time in oracle 在单个SQL语句中插入/更新/删除多个表 - insert/update/delete multiple tables in single SQL statement select-&gt; insert-&gt; delete成一个sql语句:可能吗? - select->insert->delete into one sql statement: possible? 在 Oracle 中使用单个查询将多行从一个表插入到另一个表中 - Insert multiple rows with single a query from one table into another in Oracle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM