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.
Related
We are using PostgreSQL 11 and have a query from Redmine database. It is a query that works fine in MySQL 8 but on PostgreSQL we get an error.
SELECT fixed_version_id
FROM issues WHERE
((issues.fixed_version_id IN ('current_version','2')));
ERROR: invalid input syntax for integer: "current_version"
LINE 1: ...d FROM issues WHERE ((issues.fixed_version_id IN ('current_v...
I understand that fixed_version_id is an int and that I quering strings. However, is other SQL like MySQL 8 you can do this and it actually returns values. But in PostgreSQL we get an error. Not sure if we have it setup wrong or if this is just the way PostgreSQL works?
Any help would be most appreciated thank you.
We ran this query
SELECT fixed_version_id
FROM issues
WHERE ((issues.fixed_version_id IN ('current_version','2')));
We were expecting Not to get an error.
SQL is a tightly typed language (seems MySql does not adhere to the standard). The only correction is using the correct type - in this case integer. But you can `CAST' an integer to text and compare.
SQL Standard:
WHERE ((cast (issues.fixed_version_id as text) IN ('current_version','2')));
Postgresql extension:
WHERE ((issues.fixed_version_id::text IN ('current_version','2')));
Our previous stack was JPA + Hibernate to access our postgresql database. We knew that we should cast types with cast(someUUID AS text) instead of someUUID::text. (why?)
Recently we added jooQ to generate type safe sql for more complicated statements. But unfortunately jooQ generates casts (::) which are not compatible with JPA+Hibernate.
create.renderInlined(/*...*/).replace("::", "\\:\\:") doesn't work because parameters which contain "::" are replaced as well.
Is there any way to tell jooQ to use the other syntax or just replace the casts?
Thanks in advance,
Thomas
According to TFM (Postgres docs), you use the normal concatenation operator to join two HSTOREs:
SELECT 'a=>b, c=>d'::hstore || 'c=>x, d=>q'::hstore
Result:
"a"=>"b", "c"=>"x", "d"=>"q"
However, I'm getting an error when running that exact same command:
[42883] ERROR: operator does not exist: hstore || hstore Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
The only way I can get the desired result is to do something so hackish it makes me want to cry: converting any HSTOREs I have into text first, then concatenating the text and converting back to an HSTORE. This, of course, isn't good as the docs also state that if there are duplicate keys, there's no guarantee as to which one will survive the concatenation.
Before I file a bug with the Postgres folks, can anybody else duplicate this? Version info:
select version();
PostgreSQL 9.4.5 on x86_64-unknown-linux-gnu, compiled by gcc (Debian 4.7.2-5) 4.7.2, 64-bit
select extname,extversion from pg_catalog.pg_extension;
hstore 1.3
Figured out the issue. The HSTORE extension was installed into a separate schema; I was able to call that schema by identifying it (sys.hstore), but it was still not liking the operator ||. The fix was actually pretty simple: I added sys to the search_path.
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.
I have postgreSQL 8.4+PostGIS 1.5.
I want to generate GeoJson. I do:
SELECT row_to_json(fc)
FROM ( SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features
FROM (SELECT 'Feature' As type
, ST_AsGeoJSON(lg.the_geom)::json As geometry
, row_to_json(lp) As properties
FROM parcels_temp As lg
INNER JOIN (SELECT num, cadastr FROM parcels_temp) As lp
ON lg.num = lp.num ) As f ) As fc;
But get an error:
ERROR: type "json" does not exist
LINE 4: , ST_AsGeoJSON(lg.the_geom)::json As geometry
What am I doing wrong?
There is no json data type in PostgreSQL 8.4. The type was introduced in 9.2, though a backport to 9.1 was created; see this bitbucket.
Use text. json is just a validating wrapper around the text type anyway, the interesting bit is the functions like row_to_json - which are also unavailable for 8.4.
If you can't use text - say, because you're using 3rd party code that expects json, or because you need the json functions - then it's time to upgrade PostgreSQL. 8.4 is getting pretty elderly anyway, as is PostGIS 1.5.