Is it possible to modify a table in mySql through stored procedures?

this is what I would like to do in mySQL ... I get the feeling that this is simply impossible, but I would like to be wrong ...

create procedure foo(IN MYTABLE varchar(50) , IN COLNAME varchar (50), IN MYTYPE varchar(50)) 
begin 
IF (select count(*) from information_schema.columns where table_name =MYTABLE and column_name = COLNAME) = 0 
THEN
alter table MYTABLE add column MYNAME MYTYPE; 
end;

call foo( 'table_foo' , 'column_bar' , 'varchar(100)' );
+3
source share
2 answers

I don’t know why on Earth you would like this, but it is possible:

DELIMITER //
DROP PROCEDURE foo//
CREATE PROCEDURE foo(IN MYTABLE varchar(50) , IN COLNAME varchar (50), IN MYTYPE varchar(50))
BEGIN
  SET @ddl = CONCAT('alter table ', MYTABLE, ' add column (', COLNAME, ' ', MYTYPE, ')');
  PREPARE STMT FROM @ddl;
  EXECUTE STMT;
END;
//
+6
source

The short answer is why I will do it.

. Shell scripting - - , SQL - , . 1.1, 1.4 , 1.1- > 1.2, 1.2- > 1.3 1.3- > 1.4 .

+2

Source: https://habr.com/ru/post/1703204/


All Articles