简体   繁体   中英

How to execute the other schema stored procedures?

I am working in a java web application project. 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). 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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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