我正在尝试调用过程内部的过程,但调用过程位于不同的数据库模式中,因此它是一个变量而不进行转换。
DECLARE client_database varchar(255);
SET client_database = get_database(_client_id); --this gets the schema the procedure is in
CALL client_database.client_procedure(); --then trying to call the procedure with the db schema variable however it's not translating
问题:为什么client_database
没有翻译?
答案 0 :(得分:0)
一种选择是使用13.5 Prepared SQL Statement Syntax:
mysql> DROP PROCEDURE IF EXISTS `client_procedure`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> DROP PROCEDURE IF EXISTS `server_procedure`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> DROP DATABASE IF EXISTS `client_db`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> DROP DATABASE IF EXISTS `server_db`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CREATE DATABASE IF NOT EXISTS `client_db`;
Query OK, 1 row affected (0.00 sec)
mysql> CREATE DATABASE IF NOT EXISTS `server_db`;
Query OK, 1 row affected (0.00 sec)
mysql> DELIMITER //
mysql> CREATE PROCEDURE `client_db`.`client_procedure`()
-> BEGIN
-> SELECT CONCAT('FROM `', DATABASE(), '`');
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE PROCEDURE `server_db`.`controller_procedure`()
-> BEGIN
-> DECLARE `client_database` VARCHAR(64);
->
-> -- This gets the schema the procedure is in
-> -- SET `client_database` := `get_database`(`client_id`);
-> SET `client_database` := 'client_db';
->
-> SET @`call` := CONCAT('CALL `', `client_database`, '`.`client_procedure`');
-> PREPARE `stmt` FROM @`call`;
-> EXECUTE `stmt`;
-> DEALLOCATE PREPARE `stmt`;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> CALL `server_db`.`controller_procedure`;
+-----------------------------------+
| CONCAT('FROM `', DATABASE(), '`') |
+-----------------------------------+
| FROM `client_db` |
+-----------------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
请参阅db-fiddle。