简体   繁体   English

创建嵌套存储过程

[英]Create nested stored procedure

I'm using Entity Framework Core and I want to use a stored procedure.我正在使用 Entity Framework Core,我想使用存储过程。 I create a custom SQL to create it on a migration.我创建了一个自定义 SQL 以在迁移时创建它。

The generate scrip is as follows:生成脚本如下:

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190411134055_AddGenerations_sp') THEN


                        DROP PROCEDURE IF EXISTS GetCurrentGeneration;
                        CREATE  PROCEDURE GetCurrentGeneration(IN timeOut INT, IN createBy char(255))
                        BEGIN
                               -- Stored procedure body
                        END
    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;


DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20190411134055_AddGenerations_sp') THEN

    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20190411134055_AddGenerations_sp', '2.2.4-servicing-10062');

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

But I'm getting this error但我收到这个错误

Error Code: 1357. Can't drop or alter a PROCEDURE from within another stored routine错误代码:1357。无法从另一个存储例程中删除或更改 PROCEDURE

As the error message states, you can't drop or alter a procedure.正如错误消息所述,您不能删除或更改过程。 Neither can you create a procedure.你也不能创建一个过程。 It is not supported by MySQL. MySQL 不支持它。

https://dev.mysql.com/doc/refman/8.0/en/stored-program-restrictions.html says: https://dev.mysql.com/doc/refman/8.0/en/stored-program-restrictions.html说:

Generally, statements not permitted in SQL prepared statements are also not permitted in stored programs.通常,SQL 准备语句中不允许的语句在存储程序中也不允许。 For a list of statements supported as prepared statements, see Section 13.5, “Prepared SQL Statement Syntax” .有关支持作为准备好的语句的语句列表,请参阅第 13.5 节,“准备好的SQL 语句语法”

You can follow that link to the documentation for SQL statements that are supported as prepared statements.您可以通过该链接访问支持作为准备好的语句的 SQL 语句的文档。 But the list does not include CREATE PROCEDURE.但列表包括CREATE PROCEDURE。

So you will have to create your procedure by running an SQL statement from an application or script.因此,您必须通过从应用程序或脚本运行 SQL 语句来创建您的过程。

Frankly, I don't use stored procedures in MySQL at all, because they're very inefficient.坦率地说,我根本不使用 MySQL 中的存储过程,因为它们非常低效。 I understand people coming from the culture of Microsoft SQL Server or Oracle are accustomed to using stored procedures for a lot of their work, but when using MySQL, it's more common to write code in scripts in Python, Ruby, PHP, or Perl.我知道来自 Microsoft SQL Server 或 Oracle 文化的人们习惯于在他们的很多工作中使用存储过程,但是在使用 MySQL 时,更常见的是在 Python、Ruby、PHP 或 Perl 的脚本中编写代码。 There are better editing and debugging tools for those languages.这些语言有更好的编辑和调试工具。

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

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