Mysql stored procedure to alter mutiple tables structure -


i'm building small script function or stored procedure able alter table structure in tables has no column named 'address'.

if table doesn't have column address script add it:

alter table xxxx add column address varchar(150) null after command; 

so far have found way access tables list has column 'address' on structure querying information_schema.columns table; such thing not need because need opposite, tables list has not column named 'address' on structure.

so script needs this:

alter table  (select table_name information_schema.columns table_schema = 'mydatabase'  , table_name 'mytable_%' , column_name = 'address')  add column if not exists address  varchar(150) null after command; 

something need, needs validation of column name address , attempt add column if not exists, otherwise return exception , query die.

php or other programming language cannot used because more 4000 tables upgraded , http request die before database finish operations.

another possibility handle exception in case of existence of column named 'address' able continue operations no matter if exception returns, continue query execution tables without exceptions updated....

you have build dynamic sql this. best bet

ootomh code

select 'alter table ' + tablename ' + add address varchar (150)' information_schema.tables t not exists ( select 1 information_schema.columns c c.tablename = t.tablename , c.columnname = 'address' ) 

now have generated code, can execute hand.

if isn't on daily basis, there isn't of need deploy stored proc, there?

if really want it, have plethora of options - add string variable, loop through set cursor, etc.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -