Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Mysql Alter Table With Stored Procedure

Posted on 2009-07-13
2
Medium Priority
?
616 Views
Last Modified: 2012-05-07
Hi to all,
My tables has a field and it's name 'creation_date'... and all of my tables has the field. i want to change it's name created_date with a stored procedure.(if is there any way different  solution from stored procedure it's okay too. but it has to be PURE SQL)
how can i do it?
thanks.
0
Comment
Question by:erenpasa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 17

Expert Comment

by:pssandhu
ID: 24841884
Not sure how can you do this in a single go for all tables but here is the syntax:
ALTER TABLE MyTable CHANGE OldColName  NewColName INTEGER;
So the above query changes the column name from OldcolName to NewColName and the datatype is specified which is the the original datatype.
Hope this helps.
P.
0
 

Accepted Solution

by:
erenpasa earned 0 total points
ID: 24842016
DELIMITER $$
DROP PROCEDURE IF EXISTS UpdateTables$$
CREATE PROCEDURE UpdateTables()
BEGIN
  DECLARE done INT DEFAULT 0;
  DECLARE tableName TINYTEXT;
  DECLARE cur1 CURSOR FOR SHOW TABLES;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

  OPEN cur1;

  REPEAT
    FETCH cur1 INTO tableName;
    IF NOT done THEN
      IF SUBSTRING(tableName,1,3)='fd_' THEN
            SET @ssql=CONCAT('ALTER TABLE ',tableName,' CHANGE creation_date created_date TIMESTAMP');
            PREPARE stmt FROM @ssql;
            EXECUTE stmt;
      END IF;
    END IF;
  UNTIL done END REPEAT;

  CLOSE cur1;
END$$
DELIMITER ;

CALL updateTables();
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question