Detta tog lite arbete men jag gjorde tillräckligt med justeringar. Problemet med din kod har ingenting att göra med din logik utan med MySQL Stored Procedure Language själv. När du gör dynamisk SQL har den omfattningsproblem.
Det jag gjorde var att skapa en tillfällig tabell och deponerade det returnerade värdet i den
Här är några exempeldata laddade
mysql> drop database if exists user391986;
Query OK, 1 row affected (0.08 sec)
mysql> create database user391986;
Query OK, 1 row affected (0.00 sec)
mysql> use user391986
Database changed
mysql> CREATE TABLE mytable (
-> ID BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> Name VARCHAR(255) NOT NULL,
-> Value VARCHAR(255) NOT NULL
-> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.11 sec)
mysql> INSERT INTO mytable (Name,Value) VALUES
-> ('rolando','edge'),('pamela','washington'),
-> ('dominique','wilkins'),('diamond','cutter');
Query OK, 4 rows affected (0.06 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> SELECT * from mytable;
+----+-----------+------------+
| ID | Name | Value |
+----+-----------+------------+
| 1 | rolando | edge |
| 2 | pamela | washington |
| 3 | dominique | wilkins |
| 4 | diamond | cutter |
+----+-----------+------------+
4 rows in set (0.00 sec)
mysql>
Här är den lagrade proceduren justerad för att fånga returvärdena i en temptabell
mysql> delimiter //
mysql> CREATE PROCEDURE myproc(IN myTable VARCHAR(255), IN myValue VARCHAR(255), IN myValueTwo VARCHAR(255))
-> BEGIN
-> DECLARE foundcount INT;
-> DECLARE retval VARCHAR(255);
->
-> SET @iTable=myTable;
-> SET @iValue=myValue;
-> SET @iValueTwo=myValueTwo;
->
-> CREATE TEMPORARY TABLE IF NOT EXISTS mynumber (rv VARCHAR(255)) ENGINE=MEMORY;
-> DELETE FROM mynumber;
->
-> SET retval = 'nothing retrieved';
-> SET @query = CONCAT('INSERT INTO mynumber SELECT Name FROM ', @iTable, ' WHERE Value=''', @iValue, '''');
-> PREPARE QUERY FROM @query;
-> EXECUTE QUERY;
-> DEALLOCATE PREPARE QUERY;
-> SELECT COUNT(1) INTO foundcount FROM mynumber;
-> IF foundcount = 0 THEN
-> SET @querytwo = CONCAT('INSERT INTO mynumber SELECT Name FROM ', @iTable, ' WHERE Value=''', @iValueTwo, '''');
-> PREPARE QUERY FROM @querytwo;
-> EXECUTE QUERY;
-> DEALLOCATE PREPARE QUERY;
-> END IF;
-> SELECT COUNT(1) INTO foundcount FROM mynumber;
-> IF foundcount > 0 THEN
-> SELECT rv INTO retval FROM mynumber;
-> END IF;
-> SELECT retval;
->
-> END //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql>
OK Jag ringde den lagrade proceduren tre gånger. Den första får ingenting. Den andra får det andra värdet. Den tredje får det första värdet.
mysql> CALL myproc('mytable','pamela','diamond');
+-------------------+
| retval |
+-------------------+
| nothing retrieved |
+-------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.02 sec)
mysql> CALL myproc('mytable','pamela','wilkins');
+-----------+
| retval |
+-----------+
| dominique |
+-----------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> CALL myproc('mytable','edge','wilkins');
+---------+
| retval |
+---------+
| rolando |
+---------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.02 sec)
mysql>
Ge det ett försök !!!