如何通过mysql中的单个程序更新两个不同数据库中存在的两个不同表的记录?

时间:2017-08-17 12:16:41

标签: mysql

CREATE PROCEDURE p_samp(
    IN p_id INT,IN p_table_choice VARCHAR(10)
)
  BEGIN

    CASE p_table_choice

      WHEN p_table_choice = 'A' THEN
                     USE database1;
                           update sample1 
                           SET name = 'sam'
                           WHERE id = p_id; 

      WHEN p_table_choice = 'B' THEN
                     USE database2;
                           update sample2 
                           SET name = 'sam'
                           WHERE id = p_id;

      ELSE
          BEGIN
          END;
    END CASE ;

  END;

2 个答案:

答案 0 :(得分:0)

您可以尝试:

CREATE PROCEDURE p_samp(
IN p_id INT,IN p_table_choice VARCHAR(10)
)
BEGIN

CASE p_table_choice

  WHEN p_table_choice = 'A' THEN
                       update database1.sample1 
                       SET name = 'sam'
                       WHERE id = p_id; 

  WHEN p_table_choice = 'B' THEN
                       update database2.sample2 
                       SET name = 'sam'
                       WHERE id = p_id;

  ELSE
      BEGIN
      END;
END CASE ;

END;

我尝试了一个示例程序,但它确实有效。

以下是我尝试在两个数据库learningpricing>>

之间执行类似更新的示例程序
CREATE PROCEDURE `xxxx`(A int(1))
begin
case A
when 1 then update learning.GAUL set user='OBELIX' where id=1;
when 0 then update pricing.K1 set amntIN='500' where account=1;
else
select 'DUMMY';
END CASE;
END

所以基本上第一次更新将导致一个受影响的行,第二次更新将导致2个受影响的行。

我打电话给他们:

mysql> call xxxx(1);
Query OK, 1 row affected (0.05 sec)

mysql> call xxxx(0);
Query OK, 3 rows affected (0.12 sec)

mysql> call xxxx(3);
+-------+
| DUMMY |
+-------+
| DUMMY |
+-------+
1 row in set (0.00 sec)

答案 1 :(得分:0)

以下脚本按预期工作:

mysql> DROP PROCEDURE IF EXISTS `p_samp`;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS `database2`.`sample2`,
    ->                      `database1`.`sample1`;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP DATABASE IF EXISTS `database2`;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP DATABASE IF EXISTS `database1`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE DATABASE IF NOT EXISTS `database1`;
Query OK, 1 row affected (0.00 sec)

mysql> CREATE DATABASE IF NOT EXISTS `database2`;
Query OK, 1 row affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `database1`.`sample1` (
    ->   `id` SERIAL,
    ->   `name` VARCHAR(255)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `database2`.`sample2` (
    ->   `id` SERIAL,
    ->   `name` VARCHAR(255)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO `database1`.`sample1`
    ->   (`name`)
    -> VALUES
    ->   ('sam in db1');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO `database2`.`sample2`
    ->   (`name`)
    -> VALUES
    ->   ('sam in db2');
Query OK, 1 row affected (0.00 sec)

mysql> DELIMITER //

mysql> CREATE PROCEDURE `p_samp` (
    ->   `p_id` BIGINT UNSIGNED,
    ->   `p_table_choice` CHAR(1)
    -> )
    -> BEGIN
    ->   CASE `p_table_choice`
    ->     WHEN 'A' THEN
    ->       UPDATE `database1`.`sample1`
    ->       SET `name` = 'sam'
    ->       WHERE `id` = `p_id`;
    ->     WHEN 'B' THEN
    ->       UPDATE `database2`.`sample2`
    ->       SET `name` = 'sam'
    ->       WHERE `id` = `p_id`;
    ->   END CASE;
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;

mysql> SELECT
    ->   `id`,
    ->   `name`
    -> FROM
    ->   `database1`.`sample1`;
+----+------------+
| id | name       |
+----+------------+
|  1 | sam in db1 |
+----+------------+
1 row in set (0.00 sec)

mysql> SELECT
    ->   `id`,
    ->   `name`
    -> FROM
    ->   `database2`.`sample2`;
+----+------------+
| id | name       |
+----+------------+
|  1 | sam in db2 |
+----+------------+
1 row in set (0.00 sec)

mysql> CALL `p_samp`(1, 'A');
Query OK, 1 row affected (0.00 sec)

mysql> SELECT
    ->   `id`,
    ->   `name`
    -> FROM
    ->   `database1`.`sample1`;
+----+------+
| id | name |
+----+------+
|  1 | sam  |
+----+------+
1 row in set (0.00 sec)

mysql> SELECT
    ->   `id`,
    ->   `name`
    -> FROM
    ->   `database2`.`sample2`;
+----+------------+
| id | name       |
+----+------------+
|  1 | sam in db2 |
+----+------------+
1 row in set (0.00 sec)

mysql> CALL `p_samp`(1, 'B');
Query OK, 1 row affected (0.00 sec)

mysql> SELECT
    ->   `id`,
    ->   `name`
    -> FROM
    ->   `database1`.`sample1`;
+----+------+
| id | name |
+----+------+
|  1 | sam  |
+----+------+
1 row in set (0.00 sec)

mysql> SELECT
    ->   `id`,
    ->   `name`
    -> FROM
    ->   `database2`.`sample2`;
+----+------+
| id | name |
+----+------+
|  1 | sam  |
+----+------+
1 row in set (0.00 sec)