MySQL Forums
Forum List  »  Stored Procedures

Re: Can I use a dynamic table name in stored procedure --> SOLVED
Posted by: ganesh venugopal
Date: April 24, 2008 12:57AM

Hi Martin,

What is the use of PREPARE and EXECUTE in the bellow statements because i am going to use this to write a procedure to select all from particular table, but i am going to pass the table name in the parameter dynamically and using that parameter value the select query has to perform. Please advise me how to do this.

Thank you,


Michael Gargiullo Wrote:
-------------------------------------------------------
> Beautiful!
>
> Thank you Martin.
>
> For those who would like an example of the full
> working sp:
>
>
> DELIMITER |
> create procedure test (IN Rule INT, IN RuleTable
> varchar(25), IN min INT)
> BEGIN
> DECLARE num_v INT;
> SET @num_v=-1;
> SET @dyn_sql=CONCAT('Select count(*) into @num_v
> from ',RuleTable,' where TimeStamp > (now() -
> INTERVAL ? MINUTE)');
> PREPARE s1 from @dyn_sql;
> SET @min_val=min;
> EXECUTE s1 USING @min_val;
> DEALLOCATE PREPARE s1;
> IF( @num_v < 1) THEN
> SET @dyn_ins="insert into EventEngineAlerts
> values ('',?,now(),0)";
> PREPARE s2 from @dyn_ins;
> SET @rule_val=Rule;
> EXECUTE s2 USING @rule_val;
> DEALLOCATE PREPARE s2;
> END IF;
> END;
> |
> DELIMITER ;

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: Can I use a dynamic table name in stored procedure --> SOLVED
7023
April 24, 2008 12:57AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.