SQL to update a table only if the table exists in the database

I have a mySQL database that can have a table named jason . A single database instance may not have a jason table (it would have common shared tables)

I would like to run a simple update for both databases, but an update for the jason table.

I know I can do something like

 DROP TABLE IF EXISTS `jason`; 

Is it possible to run Update something like:

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

I can't seem to work anything.

+4
source share
4 answers

Just run the update statement, if the table does not exist, it will not be corrupted and will not be corrupted.

+4
source

If you specify a table that does not exist, the update instruction will not compile. You will need to create dynamic SQL and execute it only when the table exists.

0
source

You can also refer to these mysql docs . All information about the schema is in the database, so essentially you can make any queries the same way. Examples at the bottom.

0
source

This will not fail, but if you insist, you can do the work:

 IF EXISTS (SELECT * FROM Table1) UPDATE Table1 SET (...) WHERE Column1='SomeValue' ELSE INSERT INTO Table1 VALUES (...) 
-one
source

All Articles