Mysql

程序未正確循環

  • August 12, 2022

我在一台伺服器上有幾個客戶端數據庫。它們都有一個前綴。為此,讓我們使用“piggy_”作為前綴。

IE

piggy_client_a
piggy_client_b
piggy_client_c

下面是三個小豬數據庫的程式碼並添加了一堆使用者。(用於檢測):

CREATE SCHEMA IF NOT EXISTS piggy_client_a;

CREATE TABLE piggy_client_a.user (
 id INT(11) NOT NULL AUTO_INCREMENT,
 fullname VARCHAR(30) DEFAULT NULL,
 email VARCHAR(100) DEFAULT NULL,
 PRIMARY KEY (id)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

INSERT INTO piggy_client_a.user (fullname, email) VALUES
("chris davis", "chris@something.com"),
("chris td", "chris@something.com"),
("bob sinclair", "bob@spectrum.net"),
("frank sidebottom", "frank@bighead.com");

CREATE SCHEMA IF NOT EXISTS piggy_client_b;

CREATE TABLE piggy_client_b.user (
 id INT(11) NOT NULL AUTO_INCREMENT,
 fullname VARCHAR(30) DEFAULT NULL,
 email VARCHAR(100) DEFAULT NULL,
 PRIMARY KEY (id)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

INSERT INTO piggy_client_b.user (fullname, email) VALUES
("fanny adams", "chris@elsewhere.net"),
("colin furry", "colin@something.com"),
("chris grumps", "grumpy@htom.com"),
("tracey singer", "trace@email.com");

CREATE SCHEMA IF NOT EXISTS piggy_client_c;

CREATE TABLE piggy_client_c.user (
 id INT(11) NOT NULL AUTO_INCREMENT,
 fullname VARCHAR(30) DEFAULT NULL,
 email VARCHAR(100) DEFAULT NULL,
 PRIMARY KEY (id)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

INSERT INTO piggy_client_c.user (fullname, email)VALUES
("mary qos", "mary@comms.com"),
("curtis jackson", "curtis@something.com"),
("christine motor", "chrissy@live.net"),
("tom peters", "tom@peters.org");

每個 piggy 數據庫中都有相同的模式。其中一個表是user,我想找到使用者所在的任何這些數據庫。

我寫了一個儲存過程,總體上它可以工作。然而,它只處理第一個小豬數據庫。我已經仔細檢查過,如果我將特定使用者添加到piggy_client_c數據庫中,它不會被返回。

這是我的程式碼:

DELIMITER $$
DROP PROCEDURE IF EXISTS `find_user`$$

CREATE PROCEDURE `find_user`(IN partial_name VARCHAR(20))
BEGIN

   DECLARE database_name VARCHAR(128) DEFAULT '';
   DECLARE non_found INTEGER DEFAULT 0;

   DECLARE database_list 
       CURSOR FOR 
           SELECT SCHEMA_NAME FROM information_schema.SCHEMATA 
           WHERE SCHEMA_NAME LIKE `piggy_client_%` 
           ORDER BY SCHEMA_NAME ASC;

   DECLARE CONTINUE HANDLER FOR NOT FOUND SET non_found = 1;

   OPEN database_list;

   check_table: LOOP

       FETCH database_list INTO database_name;

       IF non_found = 1 THEN 
           LEAVE check_table;
       END IF;

       SET @sql = CONCAT('SELECT "',database_name,'" AS db_name, fullname,email FROM ',database_name,'.user WHERE trim(fullname) like "%',partial_name,'%"');

       PREPARE db_statement FROM @sql;
       EXECUTE db_statement;
       DEALLOCATE PREPARE db_statement;

   END LOOP check_table;

   CLOSE database_list;

END$$

DELIMITER ;

我看不到任何會導致函式退出或不循環游標的東西,但我對此相當陌生。

或者,如果我可以“單步執行”程式碼,那將很有用,或者輸出調試資訊。

TIA,克里斯

在您的程式碼中有一個錯誤:

SELECT SCHEMA_NAME FROM information_schema.SCHEMATA 
WHERE SCHEMA_NAME LIKE `piggy_client_%` 
ORDER BY SCHEMA_NAME ASC;

用字元串分隔符替換反引號,如下所示:

SELECT SCHEMA_NAME FROM information_schema.SCHEMATA 
WHERE SCHEMA_NAME LIKE 'piggy_client_%' 
ORDER BY SCHEMA_NAME ASC;

我創建了一個db-fiddle

您還誤用雙引號作為字元串分隔符。您的插入語句應如下所示:

('fanny adams', 'chris@elsewhere.net'),

但顯然這有效,所以我還沒有解決這個問題。

引用自:https://dba.stackexchange.com/questions/315508