Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL函数中的表参数_Mysql_Function_Mysql Workbench - Fatal编程技术网

MySQL函数中的表参数

MySQL函数中的表参数,mysql,function,mysql-workbench,Mysql,Function,Mysql Workbench,基本上,我试图在mysql函数中引用我的表,因此在我的查询中,我可以说“from x”,因为在x中是函数的一个参数,因此有人可以将函数放在他们希望运行的表中 CREATE DEFINER=`root`@`localhost` FUNCTION `somefunction`(t varchar(8), num integer) RETURNS int(8) BEGIN DECLARE result integer(12); DECLARE test varchar(

基本上,我试图在mysql函数中引用我的表,因此在我的查询中,我可以说“from x”,因为在x中是函数的一个参数,因此有人可以将函数放在他们希望运行的表中

    CREATE DEFINER=`root`@`localhost` FUNCTION `somefunction`(t varchar(8), num integer) RETURNS int(8)
    BEGIN
    DECLARE result integer(12);
    DECLARE test varchar(12);
    SET result = 0;
    SET test = t;
    select integer * 5 INTO result from x;
    return result;
    END

基本上,当我执行某个函数时(thisisthetableiwant,5),我得到一个错误,说它找不到“字段列表中的测试”,因此它没有将表设置为我在参数中输入的内容,目前我有“来自x”的部分用我想要的表进行硬编码,它可以工作,但我需要制作它,这样我就可以有一个参数,以防我需要在另一个表上使用该函数

不能这样做。不能将字段的参数/变量/内容用作sql关键字。您需要将查询构建为字符串,将表名插入其中,然后执行该字符串<代码>设置foo='users';从@foo选择*from将不起作用。因此,我查看了其他类似的答案,似乎我无法做到这一点,因为我正在创建一个函数,而不是一个过程,因为我尝试创建一个concat字符串,并且我得到了“错误1336:存储函数或触发器sql语句中不允许使用动态sql”但是您的函数可以调用存储过程!是啊,太难看了。