Solved

Mysql Alter Table With Stored Procedure

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

705 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