gpt4 book ai didi

mysql - 在多个数据库上安装存储过程

转载 作者:行者123 更新时间:2023-11-29 22:31:16 25 4
gpt4 key购买 nike

有没有办法可以轻松地在多个 MySQL 数据库上同时创建存储过程?所有数据库都安装在同一个 MySQL 上。

最佳答案

在所有架构中安装

要获取架构列表,请使用showdatabase;。将此与 -- use 结合使用:

use schemaA;
-- use schemaB;
-- use schemaC;

create procedure ...

手动迭代架构,在继续操作时删除并取消注释 use 子句,检查一切是否正常。在 MySQL Workbench 中,Ctrl+Shift+Enter 是你的 friend 。

在架构子集中安装例程

通常,您不想在服务器上的所有架构中安装存储例程,而只想在子集中安装存储例程——通常由架构集定义已经安装了一些特定的存储例程。然后,as discussed on SO ,您可以使用这样的查询来获取相关模式的名称:

SELECT ROUTINE_SCHEMA FROM `information_schema`.`ROUTINES` where specific_name = 'MyRoutine'; 

验证

部署例程后,要验证它们是否存在,您可以使用如下查询:

SELECT distinct
r1.ROUTINE_SCHEMA,
case when r2.specific_name is not null then '' else '####' end as RoutineName1,
case when r3.specific_name is not null then '' else '####' end as RoutineName2,
case when r4.specific_name is not null then '' else '####' end as RoutineName3
FROM
`information_schema`.`ROUTINES` as r1
LEFT JOIN (select * from `information_schema`.`ROUTINES` where specific_name = 'RoutineName1') as r2 on r1.routine_schema = r2.routine_schema
LEFT JOIN (select * from `information_schema`.`ROUTINES` where specific_name = 'RoutineName2') as r3 on r1.routine_schema = r3.routine_schema
LEFT JOIN (select * from `information_schema`.`ROUTINES` where specific_name = 'RoutineName3') as r4 on r1.routine_schema = r4.routine_schema
where
r1.specific_name = 'FilteringRoutineName';

此查询将检查您服务器上具有例程 FilteringRoutineName 的数据库架构中是否存在 RoutineName1RoutineName2RoutineName3 。如果缺少例程,则会用 #### 标记。

当然,这仅检查例程是否存在。要验证它们的实现,您可能需要一个数据库比较工具(例如 MySQL Compare 或类似工具)。

关于mysql - 在多个数据库上安装存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29760956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com