本文介绍了MyBatis一次执行多个sql语句,这可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在1 go中执行多个sql语句。
例如我想从多个表中删除行的场景,有没有办法可以做类似的事情..

i was wondering if it is possible to execute multiple sql statements in 1 go.For example the scenario that i want to delete rows from multiple tables, is there a way i can do things like..

<delete id="delete" parameterType="String">
    DELETE FROM DUMMYTABLE_A where X=${value}
    DELETE FROM DUMMYTABLE_B where X=${value}
</delete>


推荐答案

是的,大多数数据库都允许这样做。通常,您必须使用某些内容来划分SQL语句。在PostGRES和MySQL中,它是一个分号(;)。在Microsoft SQL Server中,您应该使用关键字GO。 [2013年5月更新:从SQL Server 2012开始,您可以并且应该使用分号来分隔您的语句。在SQL Server 2012(即下一个版本及更高版本)之后,这些将是强制性的。使用GO现在是在SQL2012及更高版本中不常用的方法。 ]

Yes, most databases allow this. Usually you have to delimit your SQL statements with something. In PostGRES and MySQL it's a semicolon (;). In Microsoft SQL server you should use the keyword GO. [ May 2013 Update: As of SQL Server 2012, you can and should use semicolons to delimit your statements. After SQL Server 2012 (ie. the next version and beyond) these will be mandatory. Using GO is now the deprecated way to do things in SQL2012 and beyond). ]

MySQL / PostGRES示例:

MySQL / PostGRES example:

 DELETE FROM DUMMYTABLE_A where X=${value};
 DELETE FROM DUMMYTABLE_B where X=${value};
 DELETE FROM DUMMYTABLE_C where X=${value};

MS-SQL示例:

 DELETE FROM DUMMYTABLE_A where X=${value}
 GO
 DELETE FROM DUMMYTABLE_B where X=${value}
 GO
 DELETE FROM DUMMYTABLE_C where X=${value}

更好的数据库(即非MySQL)也将支持与BEGIN TRAN的交易/ COMMIT TRAN / ROLLBACK TRAN。使用事务,您实际上可以将所有语句批量处理为一个原子操作,如果其中一部分失败,则所有三个都将回滚。有关更多信息,请参见关于那些。

Better databases (ie. not MySQL) will also support transactions with BEGIN TRAN / COMMIT TRAN / ROLLBACK TRAN. Using transactions you can actually batch all the statements into one atomic operation, where if part of it failed, all three would be rolled back. See http://www.sqlteam.com/article/introduction-to-transactions for some more information about those.

你最需要的只是你的SQL语句之间的分号!

Most likely all you need is the semicolons between your SQL statements though!

这篇关于MyBatis一次执行多个sql语句,这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:39