Selecting NEXTVAL from H2 Database and DB2 - db2

Currently I am using H2 for integration test and DB2 for DEV and UAT.
To get the next value from a sequence, we are using Values NEXTVAL FOR SCHEMANAME.SEQNAME SQL for Db2 and which is working as expected.
However the above SQL not working in the H2 Database and we are using Select NEXTVAL ('SCHEMANAME','SEQNAME') in H2. This is not working in Db2
However I need to use same SQL query or both DB2 and H2 to get NEXTVAL.
Can we use one SQL to get NEXTVAL in both H2 and DB2?

The SQL Standard has a NEXT VALUE FOR sequenceGeneratorName expression. This expression is supported by both databases:
https://www.ibm.com/support/knowledgecenter/en/SSEPEK_12.0.0/sqlref/src/tpc/db2z_sequencereference.html
https://h2database.com/html/grammar.html#sequence_value_expression
You can test it with
VALUES NEXT VALUE FOR SCHEMANAME.SEQNAME;
Note that you need a recent version of H2 (1.4.199, for example), because old versions of H2 don't support table value constructor (VALUES …).

Related

H2 Syntax Question For How to Define Inplicit Column Name

I have a SQL syntax question, in DB2, I can write such sql:
select * from
(values(0),(1)) ones(n),
(values(0),(1)) tens(n)
but in H2, it pop error: Syntax error in sql statement:
select * from
(values(0),(1)) ones([*]n),
(values(0),(1)) tens(n)
[42000-191] 42000/42000
Can anyone give hint how to write it in H2? I think it is related with how to define implicit columns on the fly.
You're using an outdated version of H2 database. This syntax is only supported since H2 1.4.197, you need to use it or some newer version.

alternative methods for ELT in datastage?

Is ELT possible in Datastage by using join,aggregate components ( without running directly a SQL statement from a database stage or by calling a stored procedure?)
Example:
In oracle db, i have a sql as shown below.
In IBM datastage, is it possible to run this sql with etl components( without running directly a SQL statement from a database stage or by calling a stored procedure)
insert into trg1
select
cust_expense.cust_id, cust.cust_name,
cust_expense.month, sum(cust_expense.expense)
from cust_expense, cust
group by
cust_expense.cust_id, cust.cust_name,
cust_expense.month

PostgreSQL: Equivalent way to express the following Oracle SQL, HH.MI.SSXFF AM

The oracle script I'm in the process of 'converting' so it can be executed in PGAdmin4, has the following values to insert into a column of table with a data type of 'date'
to_timestamp('12-JUN-99','DD-MM-YY HH.MI.SSXFF AM')
From my understanding, FF represents Fractional Seconds.
What would be the equivalent way to represent the statement in PostgreSQL/PGAdmin 4?
SSXFF is my main concern.
I don't see how that code works in Oracle. But this should work in both Postgres and Oracle:
select to_timestamp('12-JUN-99', 'DD-MON-YY')

Query works in postgresql, but not in hsqldb

column_name is of type int[]
SELECT unnest(column_name) FROM table_name
The above query works on postgresql but not on hsqldb, even with sql.syntax_pgs=true
Hsqldb versions tried : 2.2.9 and 2.3.0
The sql that works in hsqldb is
SELECT x FROM table_name, unnest(column_name) y(x)
x and y are NOT columns of this table.
HSQLDB tries to emulate PostgreSQL's syntax and features, but like most emulations it is imperfect.
IIRC, one of the things it has a hard time with is PostgreSQL's quirky use of set-returning functions in the SELECT clause.
Use of SRFs in the SELECT clause is a weird PostgreSQL extension that's deprecated in favour of SQL-standard LATERAL queries anyway. The alternate formulation you showed:
SELECT x FROM table_name, unnest(column_name) y(x);
is the correct and preferred form. So just use that.
In general, testing on one DB then deploying to another is a recipe for pain. I strongly suggest just setting up a local PostgreSQL instance for testing instead.

How do I turn on DB2 syntax in HyperSQL (HSQL)?

I am trying to use HSQL for testing. In production, the code runs against a DB2 database. I would like to turn on the DB2 syntax of HSQL and run the production SQL against it (or as much of it as possible).
According to the documentation, in DB2 syntax mode, the nextval for expression is supported.
I've turned on DB2 syntax mode both with the connection property, sql.syntax_db2=true, and with:
set database sql syntax db2 true;
However, I am not able to use nextval for, though the syntax next value for works correctly:
values (next value for test_seq); -- returns the next value
values (nextval for test_seq); -- results in an error
The error is:
Error: user lacks privilege or object not found: NEXTVAL
SQLState: 42501
ErrorCode: -5501
Am I not correctly activating DB2 syntax mode, or am I doing something else wrong? I am logged in as SA, so can it be a privilege issue?
There is a mistake in the documentation. The DUAL table is supported the same way as in Oracle syntax, but NEXTVAL is an Oracle form used as test_seq.NEXTVAL.
The DB2 supports NEXT VALUE FOR test_seq and allows NEXTVAL as an alternative to NEXT VALUE. This will be supported in the next release of HSQLDB.