<tfoot id='0N52T'></tfoot>

      • <bdo id='0N52T'></bdo><ul id='0N52T'></ul>
    1. <small id='0N52T'></small><noframes id='0N52T'>

      <legend id='0N52T'><style id='0N52T'><dir id='0N52T'><q id='0N52T'></q></dir></style></legend>

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

        MySQL 存储函数 - 动态/变量表 &amp;列名

        MySQL Stored Functions - Dynamic/Variable Table amp; Column Names(MySQL 存储函数 - 动态/变量表 amp;列名)

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

          <legend id='d5lfW'><style id='d5lfW'><dir id='d5lfW'><q id='d5lfW'></q></dir></style></legend>
            <tbody id='d5lfW'></tbody>

                <bdo id='d5lfW'></bdo><ul id='d5lfW'></ul>
                <i id='d5lfW'><tr id='d5lfW'><dt id='d5lfW'><q id='d5lfW'><span id='d5lfW'><b id='d5lfW'><form id='d5lfW'><ins id='d5lfW'></ins><ul id='d5lfW'></ul><sub id='d5lfW'></sub></form><legend id='d5lfW'></legend><bdo id='d5lfW'><pre id='d5lfW'><center id='d5lfW'></center></pre></bdo></b><th id='d5lfW'></th></span></q></dt></tr></i><div id='d5lfW'><tfoot id='d5lfW'></tfoot><dl id='d5lfW'><fieldset id='d5lfW'></fieldset></dl></div>
                <tfoot id='d5lfW'></tfoot>
                • 本文介绍了MySQL 存储函数 - 动态/变量表 &amp;列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  今天早上我正在学习 MySQL 中的存储过程/函数/触发器,但我在尝试在查询中使用变量表和列名时遇到了一些问题.

                  I'm learning stored procedures/functions/triggers in MySQL this morning and I'm having some problems trying to use variable table and column names in queries.

                  DELIMITER |
                  USE R2R |
                  DROP FUNCTION IF EXISTS getCategoryName |
                  CREATE FUNCTION getCategoryName(LMID INT, levelNum INT)
                      RETURNS VARCHAR(255)
                      BEGIN
                  
                          DECLARE levelName VARCHAR(255) DEFAULT makeLevelName(levelNum);
                          DECLARE levelID INT;
                          DECLARE levelNameID VARCHAR(255) DEFAULT CONCAT(levelName, 'ID');
                          DECLARE ret VARCHAR(255);
                  
                          SELECT @levelNameID INTO levelID FROM LevelMaster WHERE id=LMID;
                          SELECT description INTO ret FROM @levelName WHERE id=levelID;
                          RETURN ret;
                      END;
                  |
                  DROP FUNCTION IF EXISTS makeLevelName |
                  CREATE FUNCTION makeLevelName(levelNum INT)
                      RETURNS VARCHAR(255)
                      BEGIN
                  
                          DECLARE word VARCHAR(255);
                          DECLARE ret VARCHAR(255);
                          IF levelNum=2 THEN SET word='Two';
                          ELSEIF levelNum=3 THEN SET word='Three';
                          ELSEIF levelNum=4 THEN SET word='Four';
                          ELSEIF levelNum=5 THEN SET word='Five';
                          END IF;
                  
                          SET ret=CONCAT('Level', word);
                  
                          RETURN ret;
                      END;
                  |
                  SELECT getCategoryName(347, 2) |
                  

                  这是导致我出现问题的第一个函数 (getCategoryName),我需要用 @ 标记的两个变量作为表/列名称 - 这两行:

                  It's the first function (getCategoryName) that's causing me the problems, I need the two variables marked with @ to be the table/column names - these two lines:

                  SELECT @levelNameID INTO levelID FROM LevelMaster WHERE id=LMID;
                  SELECT description INTO ret FROM @levelName WHERE id=levelID;
                  

                  如果可能的话,我想将此函数保留为函数而不是过程,但如果这是唯一的方法,我会接受过程的答案.

                  I want to keep this function as a function rather than a procedure if possible, but would accept answers for a procedure if it's the only way.

                  感谢您的帮助,

                  理查德

                  推荐答案

                  为此使用用户/全局变量以及 PREPARE &执行:

                  Use User/Global Vars for this along with PREPARE & EXECUTE:

                  SET @columnName='myColumn';
                  SET @tableName='myTable';
                  SET @whatEver='requiredValue';
                  
                  SET @query=CONCAT('SELECT ', @columnName, ' FROM ', @tableName, ' WHERE Column=', @whatEver);
                  PREPARE QUERY FROM @QUERY;
                  EXECUTE QUERY;
                  

                  还没有测试过这个 EXACT 代码,但这些代码会起作用.也必须在程序内,不能与函数或触发器一起使用,如果有人有解决方案,请发布.

                  Haven't tested this EXACT code but something along these lines will work. Also has to be inside a Procedure, cannot be used with a function or trigger, if anyone has a soloution for that then please post.

                  这篇关于MySQL 存储函数 - 动态/变量表 &amp;列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Accessing another user#39;s table within an Oracle Stored Procedure(在 Oracle 存储过程中访问另一个用户的表)
                  Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32)(超出最大存储过程、函数、触发器或视图嵌套级别(限制 32))
                  How to View Oracle Stored Procedure using SQLPlus?(如何使用 SQLPlus 查看 Oracle 存储过程?)
                  How to Pass Java List of Objects to Oracle Stored Procedure Using MyBatis?(如何使用 MyBatis 将 Java 对象列表传递给 Oracle 存储过程?)
                  Set the variable result, from query(设置变量结果,来自查询)
                  What is dynamic SQL?(什么是动态 SQL?)

                      <tbody id='lmvvG'></tbody>

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

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

                            <tfoot id='lmvvG'></tfoot>

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