<i id='Tdwp6'><tr id='Tdwp6'><dt id='Tdwp6'><q id='Tdwp6'><span id='Tdwp6'><b id='Tdwp6'><form id='Tdwp6'><ins id='Tdwp6'></ins><ul id='Tdwp6'></ul><sub id='Tdwp6'></sub></form><legend id='Tdwp6'></legend><bdo id='Tdwp6'><pre id='Tdwp6'><center id='Tdwp6'></center></pre></bdo></b><th id='Tdwp6'></th></span></q></dt></tr></i><div id='Tdwp6'><tfoot id='Tdwp6'></tfoot><dl id='Tdwp6'><fieldset id='Tdwp6'></fieldset></dl></div>

        <bdo id='Tdwp6'></bdo><ul id='Tdwp6'></ul>

    1. <legend id='Tdwp6'><style id='Tdwp6'><dir id='Tdwp6'><q id='Tdwp6'></q></dir></style></legend>

      1. <tfoot id='Tdwp6'></tfoot>

        <small id='Tdwp6'></small><noframes id='Tdwp6'>

        MySQL 中的递归存储函数

        Recursive stored functions in MySQL(MySQL 中的递归存储函数)
        <tfoot id='EPSD1'></tfoot>
              <tbody id='EPSD1'></tbody>

            <small id='EPSD1'></small><noframes id='EPSD1'>

            <legend id='EPSD1'><style id='EPSD1'><dir id='EPSD1'><q id='EPSD1'></q></dir></style></legend>

          1. <i id='EPSD1'><tr id='EPSD1'><dt id='EPSD1'><q id='EPSD1'><span id='EPSD1'><b id='EPSD1'><form id='EPSD1'><ins id='EPSD1'></ins><ul id='EPSD1'></ul><sub id='EPSD1'></sub></form><legend id='EPSD1'></legend><bdo id='EPSD1'><pre id='EPSD1'><center id='EPSD1'></center></pre></bdo></b><th id='EPSD1'></th></span></q></dt></tr></i><div id='EPSD1'><tfoot id='EPSD1'></tfoot><dl id='EPSD1'><fieldset id='EPSD1'></fieldset></dl></div>

              • <bdo id='EPSD1'></bdo><ul id='EPSD1'></ul>
                  本文介绍了MySQL 中的递归存储函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试创建一个为特定类别递归构建路径的函数

                  I'm trying to make a function that recursively builds a path for a specific category

                  CREATE FUNCTION getPath(inId INT)
                  RETURNS TEXT
                  DETERMINISTIC
                  BEGIN
                      DECLARE return_path TEXT;
                      DECLARE return_parent_id INT;
                      SELECT CONCAT('/', name) INTO return_path FROM article_categories WHERE id = inId;
                      SELECT parent_id INTO return_parent_id FROM article_categories WHERE id = inId;
                  
                      IF return_parent_id > 0 THEN
                          SELECT CONCAT(getPath(return_parent_id), return_path) INTO return_path;
                      END IF;
                  
                      RETURN return_path;
                  END
                  

                  当我尝试使用没有父级 (parent_id = 0) 的类别运行此函数时,它工作正常,但是当我尝试使用 parent_id > 0 的类别时,我得到 1424 递归存储函数和触发器是不允许的.

                  When I try to run this function with a category that has no parents (parent_id = 0) it works fine but when I try a category that has a parent_id > 0 I get 1424 Recursive stored functions and triggers are not allowed.

                  我该如何解决这个问题?我打算将此代码托管在至少应具有 MySQL 服务器版本 5.1 的常规网络托管服务上.

                  How do I work around this? I'm going to host this code on a regular web hosting service that should have at least MySQL server version 5.1.

                  在艾克·沃克 (Ike Walker) 的帮助下,我做了一个程序,但效果很好

                  After some help from Ike Walker I have made a precedure instead that works fine

                  DROP PROCEDURE IF EXISTS getPath;
                  DELIMITER //
                  CREATE PROCEDURE getPath(IN category_id INT UNSIGNED, OUT return_path TEXT)
                  BEGIN
                      DECLARE parent_id INT UNSIGNED;
                      DECLARE path_result TEXT;
                  
                      SET max_sp_recursion_depth=50;
                  
                      SELECT CONCAT('/', ac.name), ac.parent_id INTO return_path, parent_id FROM article_categories AS ac WHERE ac.id = category_id;
                  
                      IF parent_id > 0 THEN
                          CALL getPath(parent_id, path_result);
                          SELECT CONCAT(path_result, return_path) INTO return_path;
                      END IF;
                  END //
                  DELIMITER ;
                  

                  然后我用这样的东西来称呼它

                  I then use something like this to call it

                  CALL getPath(72, @temp); SELECT @temp;
                  

                  推荐答案

                  MySQL 不允许递归函数,即使你设置了 max_sp_recursion_depth.

                  MySQL does not allow recursive FUNCTIONs, even if you set max_sp_recursion_depth.

                  如果您设置 max_sp_recursion_depth,它确实允许在 PROCEDURE 中进行多达 255 次递归.

                  It does allow up to 255 recursion in a PROCEDURE if you set max_sp_recursion_depth.

                  所以我建议你用一个过程替换你的函数,为 return_path 使用一个 INOUT 变量.

                  So I recommend that you replace your function with a procedure, using an INOUT variable for the return_path.

                  这篇关于MySQL 中的递归存储函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Building a comma separated list?(建立一个逗号分隔的列表?)
                  Errors in SQL Server while importing CSV file despite varchar(MAX) being used for each column(尽管每列都使用了 varchar(MAX),但在导入 CSV 文件时 SQL Server 中出现错误)
                  How can I import an Excel file into SQL Server?(如何将 Excel 文件导入 SQL Server?)
                  Export table to file with column headers (column names) using the bcp utility and SQL Server 2008(使用 bcp 实用程序和 SQL Server 2008 将表导出到带有列标题(列名称)的文件)
                  Concat field value to string in SQL Server(将字段值连接到 SQL Server 中的字符串)
                  SQL Server Bulk insert of CSV file with inconsistent quotes(SQL Server 批量插入带有不一致引号的 CSV 文件)
                    <tbody id='oWc7v'></tbody>
                  <legend id='oWc7v'><style id='oWc7v'><dir id='oWc7v'><q id='oWc7v'></q></dir></style></legend>

                    <i id='oWc7v'><tr id='oWc7v'><dt id='oWc7v'><q id='oWc7v'><span id='oWc7v'><b id='oWc7v'><form id='oWc7v'><ins id='oWc7v'></ins><ul id='oWc7v'></ul><sub id='oWc7v'></sub></form><legend id='oWc7v'></legend><bdo id='oWc7v'><pre id='oWc7v'><center id='oWc7v'></center></pre></bdo></b><th id='oWc7v'></th></span></q></dt></tr></i><div id='oWc7v'><tfoot id='oWc7v'></tfoot><dl id='oWc7v'><fieldset id='oWc7v'></fieldset></dl></div>
                      <bdo id='oWc7v'></bdo><ul id='oWc7v'></ul>

                      <small id='oWc7v'></small><noframes id='oWc7v'>

                      <tfoot id='oWc7v'></tfoot>