views:

168

answers:

4

I have a mySQL database that might have a table named jason. A separate instance of the database may not have the jason table (it would have other tables in common)

I'd like to run a simple update against both databases but the update is for the jason table.

I know I can do something like

DROP TABLE IF EXISTS `jason`;

Is it possible to run an Update something like:

IF EXISTS `jason` UPDATE `jason` SET...

I can't seem to get anything to work.

+5  A: 

Just run the update statement, if the table didn't exist, it will fail and cause no damage.

Null303
+1 Good one, keep it simple :D
Andomar
great answer (15 char padding)
Jrud
If I run this in phpMyAdmin, or another DB client as a part of a larger set of updates it fails and stops the query. Is there a way to tell mySQL to ignore these types of errors?
Jason
A: 

If you name a non-existant table, the update statement will fail to compile. You'll need to generate dynamic SQL and execute it only when the table exists.

Andomar
A: 

You could also refer to these mysql docs. All the schema information is in a database, so essentially you can do any queries in the same fashion. Examples towards the bottom.

neouser99
A: 

It won't fail but if you insist, you can do a work around:

IF EXISTS (SELECT * FROM Table1)
   UPDATE Table1 SET (...) WHERE Column1='SomeValue'
ELSE
   INSERT INTO Table1 VALUES (...)
Erik Elkins
You can't actually use that syntax in arbitrary scripts in mysql, so it won't work.
MarkR