开发者

MySQL Cursor and Prepare Statement - What is Wrong

开发者 https://www.devze.com 2023-03-16 05:36 出处:网络
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 to0. I think I

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消