简体   繁体   English

如何在 MySQL 中创建临时程序?

[英]How to create temporary procedures in MySQL?

I am currently working on a portal application for a restaurant.我目前正在为一家餐厅开发门户应用程序。 The backend database used is Mysql and I am using Spring JDBC support for writing data access logic.使用的后端数据库是 Mysql,我使用 Spring JDBC 支持来编写数据访问逻辑。

Some of my DAOs are cluttered with multiple queries and are getting upto thousand lines of code in size.我的一些 DAO 中充斥着多个查询,并且代码多达数千行。 I think I can reduce the size and complexity by moving some of my logic into a SQL-PSM procedure (Since data binding and glue code in Java is an abomination from hell).我想我可以通过将我的一些逻辑移动到 SQL-PSM 过程中来减少大小和复杂性(因为 Java 中的数据绑定和粘合代码是地狱中的可憎之物)。

However I do not have DDL rights on the Db and hence cannot create the procedure.但是,我在 Db 上没有 DDL 权限,因此无法创建该过程。

I was wondering if there is a way to create a discard-able\\temporary procedure that you can execute directly like a DML statement without having to create it first.我想知道是否有一种方法可以创建一个可以丢弃的临时过程,您可以像 DML 语句一样直接执行该过程,而无需先创建它。

ie I want a procedure minus the create keyword, that would do my work, return the result and disappear.即我想要一个减去 create 关键字的过程,它可以完成我的工作,返回结果并消失。

Googled it a lot but couldn't find anything.谷歌搜索了很多,但找不到任何东西。 :'( Just wondering if you folks know an elegant solution. :'( 只是想知道你们是否知道一个优雅的解决方案。

I guess you are looking for anonymous code blocks / anonymous procedures.我猜您正在寻找匿名代码块/匿名程序。 Unfortunately it isn't supported in MySQL (it is supported in "big" RDBMSs such as Oracle or Sybase)不幸的是它在 MySQL 中不受支持(它在“大”RDBMS 中受支持,例如 Oracle 或 Sybase)

I would consider using some ORM framework, but I am not a Java developer, so I can't recommend any of them.我会考虑使用一些 ORM 框架,但我不是 Java 开发人员,所以我不能推荐其中任何一个。

I don't think you can.我不认为你可以。 My recommendation would be to create the "stored procedure" in Java.我的建议是在 Java 中创建“存储过程”。 Write a method that takes your inputs, performs queries and calculations, then returns the dataset.编写一个接受您的输入、执行查询和计算,然后返回数据集的方法。

You are looking for anonymous SQL blocks.您正在寻找匿名 SQL 块。 But that isn't supported by MySQL.但这不受 MySQL 支持。

The only workaround is to use non-anonymous SQL blocks, so standard named stored procedures and functions.唯一的解决方法是使用非匿名 SQL 块,因此使用标准命名存储过程和函数。

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

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