Posted By

jatubio on 01/19/11


Tagged

table conditional column


Versions (?)

Add Column Table if not exists


 / Published in: MySQL
 

Can add columns to a table checking before if there

  1. delimiter '//'
  2.  
  3. CREATE PROCEDURE addcol() BEGIN
  4. IF NOT EXISTS(
  5. SELECT * FROM information_schema.COLUMNS
  6. WHERE COLUMN_NAME='new_column' AND TABLE_NAME='usuarios' AND TABLE_SCHEMA='the_schema'
  7. )
  8. THEN
  9. ALTER TABLE `the_schema`.`the_table`
  10. ADD COLUMN `new_column` BIGINT(20) UNSIGNED NOT NULL DEFAULT 1;
  11.  
  12. END IF;
  13. END;
  14. //
  15.  
  16. delimiter ';'
  17.  
  18. CALL addcol();
  19.  
  20. DROP PROCEDURE addcol;

Report this snippet  

You need to login to post a comment.