mysql stored procedure dynamic table name

When writing a stored procedure today, a problem you want to table the most parameters, if not related to the cursor, use prepare problem can be solved, however, to use a dynamic table name in the cursor, then you have to prepare sidelined.

Set all of the wisdom, finally, use a temporary table to solve the problem.

How to implement MySQL stored procedures to process parameters used in the definition of the cursor inside the SELECT command cited as the table name

First, let's describe what the scene, such as the following example (of course, is not running correctly):

  1.  
  2. CREATE PROCEDURE `proc`(SourceDBName CHAR(50), SourceTableName CHAR(50),
  3. TargetDBName CHAR(50), TargetTemplateTableName CHAR(50))
  4. BEGIN
  5. DECLARE done INT DEFAULT 0;
  6. DECLARE FieldValue CHAR(50);
  7. DECLARE CursorSegment CURSOR FOR SELECT ... FROM SourceDBName.SourceTableName;
  8. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
  9.  
  10. OPEN CursorSegment;
  11. REPEAT
  12. FETCH CursorSegment INTO FieldValue;
  13. IF NOT done THEN
  14. ...
  15. END IF;
  16. UNTIL done END REPEAT;
  17. CLOSE CursorSegment;
  18. END$$

Examples of the above parameters are stored procedure attempts to transfer, to the inside of the cursor definition database stored procedure and table names transferred to the SELECT. However, this stored procedure at runtime MySQL will prompt "SourceDBName.SourceTableName" does not exist. That will not SourceDBName and MySQL SourceTableName two identifier as a local variable to resolve, but as a direct reference to the table.

To solve this problem, the only way is to store the above process is divided into three stored procedures. Yes, three. So this is a more complex solution.

The first stored procedure, playing the role of data collector. It receives the passed parameter database and table names, and the SELECT data into a temporary table. It should be noted, the biggest advantage is that it is temporary tables thread-safe.

Second storage procedure, based on a first temporary table stored procedure generates a cursor created, and processing the specific work.

The third storage procedure, as an entry, in turn is responsible for calling stored procedures 1 and 2 stored procedures, and provide the appropriate parameters.

Three stored procedures together, we get the following example:

  1.  
  2. CREATE PROCEDURE `proc1`(SourceDBName CHAR(50), SourceTableName CHAR(50))
  3. BEGIN
  4. DECLARE SQLStmt TEXT;
  5.  
  6. SET SQL_NOTES=0;
  7.  
  8. SET @SQLStmt = CONCAT('DROP TEMPORARY TABLE IF EXISTS tmp_table_name');
  9. PREPARE Stmt FROM @SQLStmt;
  10. EXECUTE Stmt;
  11. DEALLOCATE PREPARE Stmt;
  12.  
  13. SET @SQLStmt = CONCAT('CREATE TEMPORARY TABLE tmp_table_name SELECT ... FROM ',
  14. SourceDBName, '.',SourceTableName,' WHERE ... ');
  15. PREPARE Stmt FROM @SQLStmt;
  16. EXECUTE Stmt;
  17. DEALLOCATE PREPARE Stmt;
  18. END$$
  19.  
  20. CREATE PROCEDURE `proc2`(TargetDBName CHAR(50), TargetTemplateTableName CHAR(50))
  21. BEGIN
  22. DECLARE done INT DEFAULT 0;
  23. DECLARE FieldValue CHAR(50);
  24. DECLARE CursorSegment CURSOR FOR SELECT Period FROM tmp_table_name;
  25. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
  26.  
  27. OPEN CursorSegment;
  28. REPEAT
  29. FETCH CursorSegment INTO FieldValue;
  30. IF NOT done THEN
  31. ...
  32. END IF;
  33. UNTIL done END REPEAT;
  34. CLOSE CursorSegment;
  35. END$$
  36.  
  37. CREATE PROCEDURE `proc3`(SourceDBName CHAR(50), SourceTableName CHAR(50),
  38. TargetDBName CHAR(50), TargetTemplateTableName CHAR(50))
  39. BEGIN
  40. CALL proc1(SourceDBName, SourceTableName);
  41. CALL proc2(TargetDBName, TargetTemplateTableName);
  42. END$$
  43.  

NOTE: Before running the system parameters need to be variable "sql_notes" is set to 0, or DROP TABLE proc1 at the time would stop. Because

  1.  
  2. "SQL_NOTES = {0 | 1}
  3. If set to 1 (the default), warnings of Note level are recorded.
  4. If set to 0, Note warnings are suppressed."


Guess you like

Origin www.cnblogs.com/zhuyeshen/p/12080406.html