简体   繁体   English

如何一次执行不同的SQL语句

[英]How to execute different SQL statements at once

I know that "Batch Processing allows us to group related SQL statements into a batch and submit them with one call to the database". 我知道“批处理允许我们将相关的SQL语句分组为一个批处理,并通过一次调用将其提交给数据库”。 But my question is how to execute different SQL statements at once ie I want to insert records to Employee table, Address table , Department table with one call to database. 但是我的问题是如何立即执行不同的SQL语句,即我想通过一次调用数据库将记录插入到Employee表,Address表,Department表中。 So, is it possible? 那么,有可能吗? I'm using PostgreSQL and java. 我正在使用PostgreSQL和Java。

Group those statements into an anonymous code block and execute that. 将这些语句分组到一个匿名代码块中并执行。

See http://nixmash.com/postgresql/using-postgresql-anonymous-code-blocks/ http://nixmash.com/postgresql/using-postgresql-anonymous-code-blocks/

You can't insert into multiple tables in one statement, but you can do it effectively "at once" by using a transaction: 您不能在一个语句中插入多个表,但是可以通过使用事务“一次” 有效地做到这一点:

begin;
insert into table1 ...;
insert into table2 ...;
insert into table3 ...;
commit;

All statements within the transaction (between begin and commit ) are treated atomically - ie as if they are "one statement". 事务内的所有语句(在begincommit之间)都被原子地处理-即就像它们是“一个语句”一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM