Solved

Mysql Alter Table With Stored Procedure

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

23 Experts available now in Live!

Get 1:1 Help Now