Actual solutions for Oracle MySQL dynamic tables

Source: Internet
Author: User
Tags stored procedure example

The following article mainly describes the actual solution to Oracle MySQL dynamic tables. We all know that CRM needs to regularly export relevant data back to its CRM database (destination DB: Oracle; source DB: MySQL); but because the current growth is not very large, it will be relatively large in the future. because the Oracle MySQL database is not built by me.

I decided to log on to the table and check that no field in the table can uniquely identify a ratio record. when I export data, there will be new data input, which will obviously lead to problems. the programmer is not here. He has been using Oracle all the time and is about to play with MySQL. It seems that he has to learn how to use it now. Now he writes an Oracle MySQL stored procedure for implementation. place the table generated by the system on the next day. The system inserts data into different tables according to the time. The following is the execution script:

MySQL Stored Procedure example:

 
 
  1. declare @t_date varchar(20);  
  2. set @t_date = '(select concat('readinfo',curdate()+0))';  
  3. mysql>delimiter && 

Temporary Terminator

 
 
  1. mysql>create procedure p()   
  2. ->begin   
  3. ->select * from pet;   
  4. ->end;&&   
  5. mysql>delimiter ;   
  6. mysql> call p();  

Oracle MySQL stored procedure:

 
 
  1. CREATE TABLE readinfo20070726 (  
  2. eid int(11) ,  
  3. guid varchar(36) ,  
  4. ip varchar(20) ,  
  5. date varchar(20),  
  6. bookname varchar(60) ,  
  7. version int(11) ,  
  8. isvip int(11) ,  
  9. vipname varchar(80)  
  10. ) ENGINE=MyISAM DEFAULT CHARSET=utf8   
  11. CREATE TABLE readinfo20070727 (  
  12. eid int(11) ,  
  13. guid varchar(36) ,  
  14. ip varchar(20) ,  
  15. date varchar(20),  
  16. bookname varchar(60) ,  
  17. version int(11) ,  
  18. isvip int(11) ,  
  19. vipname varchar(80)  
  20. ) ENGINE=MyISAM DEFAULT CHARSET=utf8   
  21. CREATE TABLE readinfo20070728 (  
  22. eid int(11) ,  
  23. guid varchar(36) ,  
  24. ip varchar(20) ,  
  25. date varchar(20),  
  26. bookname varchar(60) ,  
  27. version int(11) ,  
  28. isvip int(11) ,  
  29. vipname varchar(80)  
  30. ) ENGINE=MyISAM DEFAULT CHARSET=utf8  

Stored Procedure:

 
 
  1. CREATE PROCEDURE sp_readinfo(in eid int,in guid varchar(36),in ip varchar(50))  
  2. NOT DETERMINISTIC  
  3. SQL SECURITY DEFINER  
  4. COMMENT ''  
  5. BEGIN  
  6. set @table = concat('readinfo',curdate()+0);  
  7. set @temp1 = 'insert into ';  
  8. set @temp2 = '(eid,guid,ip,date) values(';  
  9. set @value1 = eid;  
  10. set @value2 = guid;  
  11. set @value3 = ip;  
  12. set @value4 = 'now())';  
  13. set @sql_text:=concat(@temp1,@table,@temp2,@value1,',',@value2,',',@value3,',',@value4);   
  14. prepare stmt from @sql_text;  
  15. execute stmt;  
  16. end; 

In doing so, it will certainly have an impact on the performance, and it is only temporary ,,,

Appendix:

MYSQL stored procedures use dynamic SQL to create multiple tables

Example 1: Create a table named k1k k2k k3k ........

 
 
  1. mysql> delimiter //  
  2. mysql> create procedure ppp (in i int)  
  3. -> begin  
  4. -> declare k int;  
  5. -> set k=1;  
  6. -> while k<i do  
  7. -> set @t=k;  
  8. -> set @tname=concat('k',@t,'k');  
  9. -> set @dwhe='(id int,name varchar(255))';  
  10. -> set @sql_text:=concat('create table ',@tname,@dwhe);  
  11. -> prepare stmt from @sql_text;  
  12. -> execute stmt;  
  13. -> set kk=k+1;  
  14. -> end while;  
  15. -> end;  
  16. -> //  
  17. Query OK, 0 rows affected (0.00 sec)  
  18. mysql> call ppp(6)//  
  19. Query OK, 0 rows affected (0.86 sec) 

Example 2: query parameters as table names

 
 
  1. mysql> create procedure pp (tname varchar(255))  
  2. -> begin  
  3. -> set @na=tname;  
  4. -> set @sql_text:='select count(*) from ';  
  5. -> set @sql_text:=concat(@sql_text,@na);  
  6. -> prepare stmt from @sql_text;  
  7. -> execute stmt;  
  8. -> end; 

The above content is a description of the solution to the dynamic table of Oracle MySQL, hoping to help you in this regard.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.