Solved

Mysql Alter Table With Stored Procedure

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
selecting date modified field from a table 2 52
MySQL Error Code 2 25
mysql date time 14 38
MySQL Finding Duplicates in a Normalized Database 6 10
More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
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 …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

830 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