Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.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 sql-查询中无法识别存储的函数参数-(Table';population.tableName';不存在)_Mysql_Sql - Fatal编程技术网

Mysql sql-查询中无法识别存储的函数参数-(Table';population.tableName';不存在)

Mysql sql-查询中无法识别存储的函数参数-(Table';population.tableName';不存在),mysql,sql,Mysql,Sql,我做了一个函数,从数据库表中返回一个随机行 DELIMITER $$ CREATE FUNCTION randomRow(tableName varchar(200)) RETURNS VARCHAR(200) DETERMINISTIC BEGIN DECLARE count int; declare randomNum int; declare row varchar(200); declare sum int; DECLARE cur CU

我做了一个函数,从数据库表中返回一个随机行

DELIMITER $$

CREATE FUNCTION randomRow(tableName varchar(200)) RETURNS VARCHAR(200)
    DETERMINISTIC
BEGIN
    DECLARE count int;
    declare randomNum int;
    declare row varchar(200);
    declare sum int;
    DECLARE cur CURSOR FOR SELECT name FROM tableName;
    set count = (SELECT COUNT(*) FROM names_men);
    set randomNum =  round(RAND() * (count - 0) + 0);
    set sum =0;
    open cur;
    loop1: loop
        If sum = randomNum THEN
            leave loop1;
        end if;
        FETCH cur INTO row;
        set sum = sum+1;
    END LOOP loop1;
RETURN row;
END;

SET @x = randomRow("animals");
select @x;
调用此函数时,sql显示错误:

第55行出现错误1146(42S02):表“population.tableName”不存在


其原因是查询中无法识别参数tableName。如何处理这种情况?

您使用的是哪种DBMS?不能将表名作为参数传入。您需要使用动态SQL。但是,这取决于您使用的数据库,因此您应该使用正确的database.iam来标记问题。使用mysql database可能会重复