Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 0.78 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Calling stored procedure that contains dynamic SQL from Trigger
  2. -- Trigger DDL Statements
  3. DELIMITER $$
  4.  
  5. USE `TestaDataBase`$$
  6. CREATE TRIGGER `TestaDataBase`.`UpdateAuxilaryTable`
  7. AFTER INSERT ON `MainTable` FOR EACH ROW  
  8. BEGIN    
  9.     /* Here we call stored procedure with parameter id of newly inserted row. */
  10.     CALL TestProcedure('Year', 'Person', 'IdPerson', NEW.IdData);
  11. END
  12. $$
  13.        
  14. DELIMITER $$
  15. CREATE PROCEDURE `TestDataBase`.`TestProcedure` (IN attribute CHAR(64), IN tableName CHAR(64), IN IdTable CHAR(64), IN IdLastRow MEDIUMINT)
  16. BEGIN
  17. DECLARE selectedValue MEDIUMINT;
  18.  
  19. SET @statement = CONCAT('SELECT ', attribute, ' FROM ', tableName, ' WHERE ', IdTable, ' = ', IdLastRow, ' INTO selectedValue');
  20. PREPARE statementExecute FROM @statement;
  21. EXECUTE statementExecute ;
  22. ...
  23. ...
  24. END