I am trying to create a procedure in MySQL that uses a cursor and an execute. I would like to introspect the schema and update all tables that have a version column, setting the version to 0. I think I am missing a loop (looking for help here), but I am also getting an error with my dynamic SQL. Any ideas?
DELIMITER $$
CREATE PROCEDURE update_version_number(schemaName int)
BEGIN
DECLARE the_table_name VARCHAR(200);
DECLARE version_cursor CURSOR
FOR
SELECT TABLE_NAME FROM information_schema.columns WHERE tab开发者_如何学Gole_schema = "myschema" AND COLUMN_NAME = "version" AND TABLE_NAME <> "db_info";
OPEN version_cursor;
FETCH version_cursor into the_table_name;
PREPARE run_version_update From 'UPDATE ? SET version = 0';
SET @a = the_table_name;
EXECUTE run_version_update USING @a;
DEALLOCATE PREPARE run_version_update;
CLOSE version_cursor;
END
No longer an issue. I changed how I approached the problem.
精彩评论