What syntax is used to SELECT a constant field value from a JavaDB database?

♀尐吖头ヾ 提交于 2019-12-02 08:52:45

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!