MySQL Forums
Forum List  »  Stored Procedures

How to dereference variables containing table names...?
Posted by: J L
Date: July 19, 2006 12:01PM

Could someone point me to an example of using variables in stored procedures for performing actions on multiple tables? I'm able to retrieve the table names from the information_schema with a cursor and step through them no problem, so my issue is mainly in the syntax to dereference a varible within the procedure to get the correct sql statement corresponding to each table. As illustrated here, I can perform the task in a rudimentary fashion with a prepared statement, but it seems crude and surely will get ugly when the actions I want to perform get more complex. Any help is appreciated. Thanks, JL

CREATE PROCEDURE add_test_1()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE a CHAR(50);
DECLARE cur1 CURSOR FOR SELECT table_name from information_schema.tables where table_schema = "rvdb_test";
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

OPEN cur1;

REPEAT
FETCH cur1 INTO a;
SET @sql_statement =concat('alter table ',a,' add column test_1 int');
PREPARE test_statement from @sql_statement;
EXECUTE test_statement;
UNTIL done END REPEAT;

CLOSE cur1;
END

Options: ReplyQuote


Subject
Views
Written By
Posted
How to dereference variables containing table names...?
3317
J L
July 19, 2006 12:01PM


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.