简体   繁体   English

如何执行其他架构存储过程?

[英]How to execute the other schema stored procedures?

I am working in a java web application project. 我在一个Java Web应用程序项目中工作。 I have the requirement like, i want to invoke the other schema stored procedure to execute in some other schema. 我有这样的要求,我想调用其他架构存储过程以在某些其他架构中执行。

But, when i am trying this approach, it's showing error like the table already exists(contains in SP). 但是,当我尝试这种方法时,它显示error like the table already exists(contains in SP). I can understand what the issue is, it's calling the stored procedure in same schema itself where the stored procedure was created. 我可以理解问题所在,它在创建存储过程的同一模式中调用存储过程。

Following is the detailed illustration 以下是详细说明

 1. Schema1(Subscription) - Stored procedure created is here
 2. Schema2(Subscription1)- Executing the stored procedure here like following

    > USE Subscription1;  
    > CALL subscription.createcorporatedbproc();

Please help me to know about this issue. 请帮助我了解此问题。

I have modified your SP you can create it in one schema and call it from there, you just need to pass database name as parameter in which you need to create the table. 我已经修改了您的SP,您可以在一个模式中创建它并从那里调用它,您只需要将数据库名称作为参数传递给您即可在其中创建表。

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `createCorporateDBProc`(IN DBName CHAR(100))
BEGIN 

     SET @DBName = DBName;
     SET @varSQL = CONCAT('CREATE TABLE IF NOT EXISTS ',@DBName,'.xxxx( ORDER_ORDER_ID BIGINT(20) NOT NULL, INFO_ID BIGINT(20) NOT NULL, OFFER_ID BIGINT(20) NOT NULL )  ENGINE=INNODB DEFAULT CHARSET=latin1;' );
     PREPARE stmt FROM @varSQL;
     EXECUTE stmt ;
     DEALLOCATE PREPARE stmt;

     /** If you need to create more tables , Replicate these 4 lines with your table structure **/
     SET @varSQL = CONCAT('CREATE TABLE IF NOT EXISTS ',@DBName,'.yyyy( ID BIGINT(20) NOT NULL, InfoID BIGINT(20) NOT NULL, TagID BIGINT(20) NOT NULL )  ENGINE=INNODB DEFAULT CHARSET=latin1;' );
     PREPARE stmt FROM @varSQL;
     EXECUTE stmt ;
     DEALLOCATE PREPARE stmt;
END $$

DELIMITER ;

Testing CALL statement , I will create a new database and then i will call SP it to create the table in it. 测试CALL语句,我将创建一个新数据库,然后将其调用SP以在其中创建表。

mysql> create database mydb_test;
Query OK, 1 row affected (0.00 sec)

mysql> CALL createCorporateDBProc('mydb_test');
Query OK, 0 rows affected (0.24 sec)

mysql> show tables from mydb_test;
+---------------------+
| Tables_in_mydb_test |
+---------------------+
| xxxx                |
+---------------------+
1 row in set (0.00 sec)

UPDATE on 26 Jan,2014 2014年1月26日更新

Create a sample database and create all tables which you need in that sample database, and then you can use this sample database to create other databases 创建一个示例数据库,并在该示例数据库中创建所需的所有表,然后可以使用此示例数据库创建其他数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM