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
歡迎來到Stack Overflow。您可以使用工具欄按鈕格式化源代碼。這次我爲你做了。 – 2012-07-30 09:55:28