简体   繁体   English

执行存储在列中的 SQL 查询时出现 MYSQL 错误

[英]MYSQL error while executing SQL query stored in a column

I have two tables,我有两张桌子,

execution:执行:

execution_id | order_id | execution_date
   1             1             2014-03-16
   2             1             2014-03-17

and queries:和查询:

query_name | code
  CNT_EXEC |  SELECT COUNT(execution_id) FROM `execution` 

We have query defined in a column above.我们在上面的列中定义了查询。 I am trying to execute this query using my code below;我正在尝试使用下面的代码执行此查询;

DELIMITER //

DROP PROCEDURE IF EXISTS query_execute //
CREATE PROCEDURE query_execute()

BEGIN
    DECLARE finished INTEGER DEFAULT 0;
    DECLARE s_query varchar(255);

    DECLARE c_queries CURSOR FOR 
    SELECT code FROM queries;/*since there are more than one queries in the actual query table*/
    
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
    
    OPEN c_queries;

    getquery: LOOP
        FETCH c_queries INTO s_query;
    
        IF finished = 1 THEN 
           LEAVE getquery;
        END IF;
    
        /*run the query*/
        SET @sql = s_query;
        PREPARE stmt FROM  @sql;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
    
    END LOOP;
    
END //

DELIMITER ;

 /*next I am trying to invoke the procedure and find the results*/

CREATE PROCEDURE queriesExecution()
BEGIN

SELECT query_name, query_execute() AS val
  FROM queries;

END

Going by the MySQL documentation, it seems I am using the correct procedure to create the procedure.But here is the error I am getting;根据 MySQL 文档,我似乎正在使用正确的程序来创建程序。但这是我遇到的错误;

ERROR 1064 (42000) in the pre-written template: You have an error in your SQL syntax;预先编写的模板中的 ERROR 1064 (42000):您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5`.检查与您的 MySQL 服务器版本相对应的手册,以获取在第 5 行的 '' 附近使用的正确语法。

As i already wrote in my comment, your approach doesn't work .正如我在评论中已经写的那样,您的方法不起作用。

But you can change it a bit但是你可以稍微改变一下

First:第一的:

Change your queries, because you need a result更改您的查询,因为您需要一个结果

CREATE TABLE queries (
  `query_name` VARCHAR(8),
  `code` VARCHAR(355)
);

INSERT INTO queries
  (`query_name`, `code`)
VALUES
  ('CNT_EXEC', 'SELECT COUNT(execution_id) INTO @result  FROM `execution`');

As you can see the result is put into a user defined variable.如您所见,结果被放入用户定义的变量中。 so that ot can be used in the INSERT INT myresult以便 ot 可以在INSERT INT myresult

DELIMITER //

DROP PROCEDURE IF EXISTS query_execute //
CREATE PROCEDURE query_execute()

BEGIN
    DECLARE finished INTEGER DEFAULT 0;
    DECLARE s_query varchar(255);

    DECLARE c_queries CURSOR FOR 
    SELECT code FROM queries;/*since there are more than one queries in the actual query table*/
    
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
    CREATE TEMPORARY TABLe myresults(`query` varchar(255), result INT);
    OPEN c_queries;

    getquery: LOOP
        FETCH c_queries INTO s_query;
    
        IF finished = 1 THEN 
           LEAVE getquery;
        END IF;
    
        /*run the query*/
        SET @sql = s_query;
        PREPARE stmt FROM  @sql;
        EXECUTE stmt;
        INSERT INTO myresults VALUES (s_query,@result);
        DEALLOCATE PREPARE stmt;
    
    END LOOP;
    SELECT * FROM myresults;
    DROP TEMPORARY TABLE myresults;
END //

DELIMITER ;

when you now call CALL query_execute();当你现在调用CALL query_execute();

you get你得到

query                                                        result
SELECT COUNT(execution_id) INTO @result  FROM `execution`    2

Of course as you add queries, you will get more rows.当然,当您添加查询时,您将获得更多行。

this now assumes you get only INTEGER back in your queries, if not you must change the datatype.现在假设您只在查询中返回 INTEGER,否则您必须更改数据类型。

This also only works, because your query returns only 1 result, if get more rows you can't use the user defined variables and you have to look for another approach.这也仅适用,因为您的查询仅返回1 个结果,如果获得更多行,您将无法使用用户定义的变量,您必须寻找另一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM