我有数十万行文字需要插入并且有几百行需要删除。文本文件如下所示:
i 01 ppp
i 02 vvv
i 45 bbb
...
d 05
i 09 mmm
i 21 jjj
....
“i”使INSERT失效,“d”表示删除DELETE,其余的文本是需要处理的数据。
insert into t1 (id, name)values(?, ?);
delete from t1 where id = ?;
我曾经为这些“i”头行读了1000行,addBatch()和executeBatch(),最后处理了那些“d”头行。 是否有可能添加混合插入和删除批处理,然后执行它们?
感谢。
答案 0 :(得分:0)
您需要2个多行语句。
INSERT INTO t1 (id, name) VALUES (?, ?), (?, ?) ...;
DELETE FROM t1 WHERE id IN (?, ?, ...);
如果你想要原子操作,锁定表(语句取决于你的数据库)
答案 1 :(得分:0)
根据相关数据库的不同,您可以尝试使用merge
语句
查看Oracle's 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);