MySql run DDL conditionally

Hello,

Many users use my softwares (on their local machines) and the databases are not at the same level. There are people who haven't upgraded the software (and the database) for more than one year then suddenly decide to upgrade.

For every software version I save the MySql structure changes but I might commit errors too so I need a "conditional" structure change especially for

Alter table xxx add field1 varchar(200);

In the case above if the field exists it will raise an error and the next command won't finish.
I know that I can check it in the INFORMATION_SCHEMA but I wonder how can I put this together in an Sql statement.
E.g.
SELECT count(*) into @isExists FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA="xxx" and TABLE_NAME = "yyy" AND COLUMN_NAME = "zzz";

and then check the variable somehow?

I want the same for create tables as well.

starhuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenData Warehouse Architect / DBACommented:
Hi starhu,

Do the databases run on linux/unix servers?  If so, it's nearly trivial to read the information schema and pass the result back to the shell.  Then the shell can make the determination of what to execute.

-- File ColumnExists.sql
SELECT count(*) into @isExists FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA="xxx" and TABLE_NAME = "yyy" AND COLUMN_NAME = "zzz";

-- File Somejob.sh
#!/bin/sh
 
Exists=`mysql < ColumnExists.sql`
if [[0] == [$Exists]];then
  # do something...
fi



Kent
0
starhuAuthor Commented:
Hi Kent,

No, I use it on Windows, and because of that I need a solution that is completely in Sql :-(
0
Kent OlsenData Warehouse Architect / DBACommented:

Ok.

Well, you'll need to write in some kind of control language.  O/S scripting, php, stored procedure, etc.

DOS scripting is ugly, so I suggest that you stay away from it.


Kent
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

starhuAuthor Commented:
Can't I check it in pure Sql?
0
Kent OlsenData Warehouse Architect / DBACommented:
I don't believe that SQL has the built-in structure to test the condition, and then execute/skip SQL based on the result of that test.

Hence the need to write it in a different language.


Kent
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
starhuAuthor Commented:
It didn't solve my problem it only gave directions
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.