首页 文章

在shell脚本中使用mysql存储过程的关联数组

提问于
浏览
0

这是我的输入表:

queue_id | queue_name|
----------------------
qid1     |  qname1   |
----------------------

我的输出应该是

key      | value     |
----------------------
queueId  | qid1      |
queueName| qname1    |
----------------------

map [queue_id] = queueId map [queue_name] = queueName

这是我的脚本

#!/bin/bash
    keyPair[queue_name]="queueName"
    keyPair[queue_id]="queueId"
    .....
    CREATE PROCEDURE queue_migration()
    BEGIN
    DECLARE rowcount INT DEFAULT 0;
    DECLARE colcount INT DEFAULT 0;
    DECLARE i INT DEFAULT 0;
    DECLARE j INT DEFAULT 0;
    DECLARE col_name VARCHAR(255);
    DECLARE col_names CURSOR FOR
      SELECT column_name
      FROM INFORMATION_SCHEMA.COLUMNS
      WHERE table_name = 'table_name' AND table_schema = 'dbname'
      ORDER BY ordinal_position;
    Get the row count into rowCount
    Get the column count into colCount
    SET i = 0;
    WHILE i= colcount THEN
                    CLOSE col_names;
                    LEAVE the_loop;
               END IF;
               FETCH col_names INTO col_name;

               INSERT INTO logy SELECT CONCAT(\"See ${keyPair[\",col_name,\"]} here\");
               SET j

         = j + 1;
                END LOOP

     the_loop;
            SET i = i + 1;
    END WHILE;

我能够正确地看到col_name(queue_id和queue_name) . 但是映射失败了 . 当我执行$ {keyPair ['queue_name']时,我正在获取queueName . 但$ {keyPair [col_name]}为空 . 建议请 .

1 回答

  • 0

    为了在存储例程中进行查询,您需要 prepareexecute 它 . 不希望以你希望的方式进行替换 . 就像是:

    SET @stmt = CONCAT( 'INSERT INTO logy SELECT CONCAT("See ${keyPair["',
                        col_name,
                        '"]} here');
    PREPARE stmt FROM @stmt;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

    (如果不太正确,请道歉 . )

相关问题