程序错误

时间:2012-07-30 09:44:15

标签: mysql procedure

DELIMITER $$

CREATE PROCEDURE `SearchUserX`( IN columnName VARCHAR( 28 ) , IN search INT )
BEGIN
       DECLARE finished INT DEFAULT FALSE ;
       DECLARE tableName VARCHAR ( 28 ) ;
       DECLARE stmtFields TEXT ;

    DECLARE tableNames CURSOR FOR
              SELECT DISTINCT`TABLE_NAME` FROM `information_schema`.`COLUMNS`
              WHERE `COLUMN_NAME` = columnName AND TABLE_SCHEMA = 'comments' ORDER BY `ORDINAL_POSITION` ;
       DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = TRUE;
       SET stmtFields = '' ;
       OPEN tableNames;
       readTables: LOOP
              FETCH tableNames INTO tableName ;
              IF finished THEN
                     LEAVE readTables ;
              END IF;
              SET stmtFields = CONCAT(
                     stmtFields , IF ( LENGTH( stmtFields ) > 0 , ' AND' , ''  ) ,
                     ' `', tableName ,'`.`' , columnName , '`= ' , search ,'') ;
       END LOOP;
       SET @stmtQuery=CONCAT('SELECT * FROM `' , tableName , '` WHERE ' , stmtFields ) ;
       PREPARE stmt FROM @stmtQuery ;
       EXECUTE stmt ;
       CLOSE tableNames ;
END

我有错误可以帮助我:

0   11:42:47    call SearchUserX('UserId',2)    Error Code: 1054. Unknown column 'TabExample.UserId' in 'where clause'

或我在clausure stmtFields

中遇到语法问题

1 个答案:

答案 0 :(得分:0)

错误说:

  

'where子句'

中的未知列'TabExample.UserId'

您需要在表UserId中添加列TabExample