问题
I'm using UNION ALL
to combine the results of several SELECT
queries into one ResultSet
. I use constant field values to identify which statement produced each row. This works well with MySQL, but Java DB is throwing SQLSyntaxErrorException
, pointing at the comma after the first constant field value.
What syntax is used to select constant field values in SELECT
queries against JavaDB databases?
The first SELECT
has ~100 columns with varying data types, and the following SELECT
s are padded to match the correct number of columns. I've it here. The statements currently look like this:
select
(0, ID, NAME_FIRST, NAME_LAST)
from person
where ID=500
union all
select
(1, COMMTYPE_ID, NULL, NULL)
from person_commtype
where PERSON_ID=500
Which throws the following exception:
java.sql.SQLSyntaxErrorException: Syntax error: Encountered "," at line 2, column 7.
I have also tried casting the value, and removing the parentheses, like so:
select
(cast(0 as integer), ID, NAME_FIRST, NAME_LAST)
from person
where ID=500
select
0, ID, NAME_FIRST, NAME_LAST
from person
where ID=500
回答1:
Remove the brackets from around your list of fields in the SELECT
clause.
select
0, ID, NAME_FIRST, NAME_LAST
from person
where ID=500
union all
select
1, COMMTYPE_ID, NULL, NULL
from person_commtype
where PERSON_ID=500
回答2:
The problem is that Apache Derby doesn't support
select null from test
Instead you have to cast the null to the right type:
select cast(null as varchar(255)) from test
So the query would look like:
select
0, ID, NAME_FIRST, NAME_LAST
from person where ID=500
union all
select
1, COMMTYPE_ID,
cast(NULL as varchar(255)),
cast(NULL as varchar(255))
from person_commtype where PERSON_ID=500
You also have to remove the brackets around your column list, because that's not standard SQL syntax.
回答3:
Try putting your numbers in single quotes.
select
('0', ID, NAME_FIRST, NAME_LAST)
from person
where ID=500
union all
select
('1', COMMTYPE_ID, NULL, NULL)
from person_commtype
where PERSON_ID=500
来源:https://stackoverflow.com/questions/10454413/what-syntax-is-used-to-select-a-constant-field-value-from-a-javadb-database