Solved

Mysql Alter Table With Stored Procedure

Posted on 2009-07-13
2
605 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now