RE: conditional statements

52 views
Skip to first unread message

Equ Zephyr

unread,
Sep 5, 2012, 12:43:31 PM9/5/12
to h2-da...@googlegroups.com
I think you should be able to achieve the desired functionality in a reusable manner by writing a stored procedure / user defined function in Java as shown in the Features section of the documentation.

From: Igal
Sent: 9/4/2012 2:55 PM
To: h2-da...@googlegroups.com
Subject: conditional statements

hi,

I'm trying to create objects only if they don't exist, but I want to use an IF-THEN statement block.  in MSSQL I would do something like:

IF NOT EXISTS( 
  SELECT  *
FROM    INFORMATION_SCHEMA.TABLES
WHERE   TABLE_NAME      = 'TableName'
   
AND TABLE_SCHEMA    = 'public'
) BEGIN
... some sql statements here
END;

is there an equivalent H2 syntax?

--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To view this discussion on the web visit https://groups.google.com/d/msg/h2-database/-/M4XfYlf-nZEJ.
To post to this group, send email to h2-da...@googlegroups.com.
To unsubscribe from this group, send email to h2-database...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/h2-database?hl=en.

Igal

unread,
Sep 5, 2012, 2:36:08 PM9/5/12
to h2-da...@googlegroups.com
I considered that approach but I am working on a project that will accept different types of database engines, e.g. H2, MSSQL, MySQL, etc., so I was hoping to put the SQL code for creating objects in a single SQL script.

thanks though,


Igal

Equ Zephyr

unread,
Sep 5, 2012, 6:28:34 PM9/5/12
to h2-da...@googlegroups.com
I don't think there's any way you are going to be able to do that in a way that is portable across databases because it's not standard SQL. For example, it's possible to do the same thing in DB2, but the syntax is different than in MSSQL. (In DB2 it's something like "IF NOT EXISTS (...) THEN (...) ) It's also different in MySQL than it is in either MSSQL or DB2.

So really, I think this is a case where you are going to have to determine what database the user is using at runtime (possibly by checking the class name of the driver) and then use conditional logic in the Java code itself to perform the required functions.


Date: Wed, 5 Sep 2012 11:36:08 -0700
From: ig...@getrailo.org
To: h2-da...@googlegroups.com
Subject: Re: conditional statements
To view this discussion on the web visit https://groups.google.com/d/msg/h2-database/-/bFwVuEsWRo0J.
Reply all
Reply to author
Forward
0 new messages