SQL ALTER TABLE

Asked

Viewed 68 times

0

I need to make some changes in my bank, but before adding a new column I need to check if it already exists, if there is no create new column if there is no column then create

Command found to check column existence

SELECT COUNT(COLUMN_NAME) AS resultado FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'procedimentos_planos_tratamentos' AND  COLUMN_NAME = 'tipo';

if the return is 1 do nothing and if the return is 0 perform following procedure

ALTER TABLE procedimentos_planos_tratamentos
ADD COLUMN tipo VARCHAR(1);
  • Here are some ways to do this https://stackoverflow.com/questions/24571611/mysql-alter-table-if-column-not-exists

1 answer

0

I was able to solve the problem as follows

    IF (SELECT COUNT(COLUMN_NAME) AS resultado FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = 'procedimentos_planos_tratamentos' AND  COLUMN_NAME = 'tipo') THEN
    BEGIN
        ALTER TABLE procedimentos_planos_tratamentos
        ADD COLUMN tipo VARCHAR(1);  
  END;
  END IF;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.