Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conditional statements in derby

Tags:

database

derby

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].

like image 274
Fakrudeen Avatar asked Feb 23 '10 10:02

Fakrudeen


People also ask

Where Clause in 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.

What are the key characteristics of Apache Derby?

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.

Does Derby use SQL?

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.

How do you create a schema in Derby?

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.


2 Answers

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...

like image 125
lampShaded Avatar answered Nov 15 '22 09:11

lampShaded


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', ...)

like image 39
Luis Daniel Mesa Velasquez Avatar answered Nov 15 '22 11:11

Luis Daniel Mesa Velasquez