问题描述
今天早上我正在学习 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.
这是导致我出现问题的第一个函数 (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:
如果可能的话,我想将此函数保留为函数而不是过程,但如果这是唯一的方法,我会接受过程的答案.
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:
还没有测试过这个 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 存储函数 - 动态/变量表 &列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!