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'
或者我在分句中的语法有问题stmtFields