Reputation: 15335
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.
Upvotes: 6
Views: 7223
Reputation: 629
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 (...)
Upvotes: -2
Reputation: 1827
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.
Upvotes: 0
Reputation: 238116
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.
Upvotes: 0
Reputation: 1042
Just run the update statement, if the table didn't exist, it will fail and cause no damage.
Upvotes: 2