It looks like derby doesn't support conditional statements [IF]. How do we write
if exists (select 1 from customers where name='somename')
update customers ...
else
insert into customers ...
in derby? Derby doesn't have 'replace into' of mysql either.
Note: I am using derby as unit-testing replacement for mysql [that is production will use mysql and unit-test will use derby].
The WHERE clause is used in the SELECT, DELETE or, UPDATE statements to specify the rows on which the operation needs to be carried out.
Some key features include: Derby has a small footprint -- about 3.5 megabytes for the base engine and embedded JDBC driver. Derby is based on the Java, JDBC, and SQL standards. Derby provides an embedded JDBC driver that lets you embed Derby in any Java-based solution.
Derby is a full-featured, open-source relational database management system (RDBMS) implemented in Java and as the name suggests it is developed by Apache Software Foundations. It is based on Java, JDBC and SQL standards. Derby is easy to install, deploy, and use.
A schema is created in Derby with CREATE SCHEMA statement. If we connect to a database, the user name that we have provided in the connection URL becomes the current schema of the connection. All database objects will be created within this schema. The current schema can be changed with the SET SCHEMA statement.
What about http://db.apache.org/derby/docs/10.2/ref/rrefcasenullif.html#rrefcasenullif?
CASE
WHEN 1 = 2 THEN 3
WHEN 4 = 5 THEN 6
ELSE 7
END
So maybe you can try something like:
CASE
WHEN select 1 from customers where name='somename' = 1 THEN update...
ELSE insert...
END
I have no idea if that would work but it seems like a start. Good luck!
edit: After trying a few of these things out...I don't know if this will really help you. It doesn't seem like you can fire switch between SELECT and INSERT; it has to be one or the other and the CASE goes inside. What you want to do may or may not be possible...
I know this might be too late but in case anyone is still looking for it:
MERGE
https://issues.apache.org/jira/browse/DERBY-3155
For Example:
MERGE INTO customers USING SYSIBM.SYSDUMMY1 ON customers.name='somename'
WHEN MATCHED THEN UPDATE SET name = 'someothername', ...
WHEN NOT MATCHED THEN INSERT(id, name, ...) VALUES (DEFAULT, 'someothername', ...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With