存储过程中的Mysql错误

时间:2010-03-24 10:46:00

标签: mysql

此存储过程用于搜索数据库中的所有表和列。

DELIMITER $$
DROP PROCEDURE IF EXISTS get_table $$
CREATE
    /*[DEFINER = { user | CURRENT_USER }]*/
    PROCEDURE `auradoxdb`.`get_table`(in_search varchar(50))
READS SQL DATA
BEGIN
DECLARE trunc_cmd VARCHAR(50);
DECLARE search_string VARCHAR(250);
DECLARE db,tbl,clmn CHAR(50);
DECLARE done INT DEFAULT 0;
DECLARE COUNTER INT;
DECLARE table_cur CURSOR FOR

SELECT concat(SELECT COUNT(*) INTO @CNT_VALUE 
                FROM `’,table_schema,’`.`’, table_name,’` 
               WHERE `’, column_name,’` REGEXP ”’,in_search,”’
             )
      , table_schema
      , table_name
      , column_name
  FROM information_schema.COLUMNS
 WHERE TABLE_SCHEMA NOT IN (‘information_schema’,'test’,'mysql’);

 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
# #Truncating table for refill the data for new search.
PREPARE trunc_cmd FROM “TRUNCATE TABLE temp_details;”
EXECUTE trunc_cmd ;
OPEN table_cur;
table_loop:LOOP
 FETCH table_cur INTO search_string,db,tbl,clmn;
# #Executing the search
SET @search_string = search_string;
SELECT  search_string;
 PREPARE search_string FROM @search_string;
 EXECUTE search_string;
SET COUNTER = @CNT_VALUE;
 SELECT COUNTER;
IF COUNTER>0 THEN
# # Inserting required results from search to tablehhh
 INSERT INTO temp_details VALUES(db,tbl,clmn);
 END IF;
 IF done=1 THEN
 LEAVE table_loop;
 END IF;
END LOOP;
 CLOSE table_cur;
# #Finally Show Results
 SELECT * FROM temp_details;
 END $$

DELIMITER ;

执行此操作时发生以下错误..

 Error Code : 1064
 You have an error in your SQL syntax; check the manual that corresponds to
 your MySQL server version for the right syntax to use near 'SELECT COUNT(*) 
 INTO @CNT_VALUE FROM `’,table_schema,’`.`’,table_name,’`' at line 12
 (0 ms taken)
可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

您似乎正在尝试为每个表动态构建SQL语句。为此,您需要引用文本部分,就像它是普通字符串并连接动态部分一样。用以下内容替换查​​询应该有效:

SELECT concat('SELECT COUNT(*) INTO @CNT_VALUE FROM ',
              table_schema,'.', table_name, 
              ' WHERE ', column_name,' REGEXP ''',in_search,''''
         )
  , table_schema
  , table_name
  , column_name
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA NOT IN ('information_schema','test','mysql');