sql - Rename column for all tables in mysql database -
i need rename column tables in databse. list of columns using query:
select table_name information_schema.columns column_name= <column name>;
but how rename simple possible , not write
alter table <table name >rename column <old name> <new name>;
for each table.
i've tried write procedure:
delimiter $$ drop procedure if exists renamecolumn $$ create procedure renamecolumn(in oldname tinytext, in newname tinytext) begin declare @name varchar(255); declare exit_loop boolean; declare tablename_cursor cursor select table_name information_schema.columns column_name=oldname; declare continue handler not found set exit_loop = true; open tablename_cursor; rename_loop: loop fetch tablename_cursor @name; alter table @name rename column oldname newname; if exit_loop leave rename_loop; end if; end loop rename_loop; end $$ delimiter;
but have following error: have error in sql syntax; check manual corresponds mysql server version right syntax use near '@name varchar(255);
declare exit_loop boolean;
declare ta' @ line 3
could please me resolve issue
you can try this.
select concat( 'alter table ', table_name, ' rename column ', column_name, ' new_', column_name, ';') rename_script information_schema.columns table_schema = 'your_db'
Comments
Post a Comment