When I am removing a key from a HSTORE, I get the error 'Unexpected end of string':
DB=# UPDATE mytable SET properties = properties - 'key' where label = '9912345678';
ERROR: Unexpected end of string
LINE 1: UPDATE mytable SET properties = properties - 'key' where ...
When I explicitly cast that string, it does work:
DB=# UPDATE mytable SET properties = properties - 'key'::text where label = '9912345678';
UPDATE 1
Why does it give this error message? Isn't 'key' a TEXT column? Or at least a string with a expected end?
The operator is overloaded so the right-hand operand should be explicitly cast. Note the example usage for text, text[] and hstore in the documentation:
'a=>1, b=>2, c=>3'::hstore - 'b'::text
'a=>1, b=>2, c=>3'::hstore - ARRAY['a','b']
'a=>1, b=>2, c=>3'::hstore - 'a=>4, b=>2'::hstore
In the statement
UPDATE mytable SET properties = properties - 'key' where label = '9912345678';
the string 'key' may be resolved as text or hstore. The first choice of the hstore algorithm is hstore, so the statement is resolved to
UPDATE mytable SET properties = properties - 'key'::hstore where label = '9912345678';
and raises the error
ERROR: Unexpected end of string
though the error message could be more informative, like Unexpected end of string while parsing hstore constant.
Related
DataBase: R2DBC Postgres
I have a column model_id at table with type: uuid[].
create table t_job
(
id uuid default gen_random_uuid() not null
primary key,
model_id uuid[] not null,
// -- ANOTHER COLUMN -- //
);
I need compare values from column model_id with Set<UUID>
#Query("""
SELECT case
WHEN COUNT(j) >= 1
THEN true
ELSE false
END
FROM t_job AS j
WHERE j.model_id IN :modelIdSet
AND j.state = 'done'
AND j.output_format = 'COLLISION'
""")
Mono<Boolean> isCollisionJobDoneBySeveralModelsId(String modelIdSet);
OUTPUT:
"debugMessage": "executeMany; bad SQL grammar [ SELECT case\n WHEN COUNT(j) >= 1\n THEN true\n ELSE false\n END\n FROM runner_processing_service.t_job AS j\n WHERE j.model_id IN :modelIdSet\n AND j.state = 'done'\n AND j.output_format = 'COLLISION'\n]; nested exception is io.r2dbc.postgresql.ExceptionFactory$PostgresqlBadGrammarException: [42601] syntax error at or near "$1""
How correct insert and compare values from uuid[] column with Set<UUID>
I try convert Set to String type and give that string to repository`s method, but it is not work to.
This query is work correct from console
enter image description here
I am using DB2 LUW and want to a assign a result of a With clause to a variable in a stored procedure.
I got the exception
{0:0} An unexpected token "AS" was found following "l = (WITH BASE". Expected tokens may include: "JOIN".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.28.11
Is it possible to assign the result on this way or should I have to solve it with a cursor?
DECLARE result CLOB(8M);
SET result = (WITH BASE AS (
xxx
)
SELECT JSON_ARRAY (select json_objects FROM ITEMS format json) FROM SYSIBM.SYSDUMMY1);
Use instead the syntax style:
with ctename AS ( ... ) SELECT ... INTO ... FROM ctename;
I have to update a newly created metadata table (which holds a 1-1 relations with a person table) in PostgreSQL (>=12):
UPDATE public.metadata m
SET print_status =
CASE
WHEN p.print IS NOT NULL THEN 'done'
ELSE 'not done'
END
FROM public.people p
WHERE m.fk_people_id = p.id;
but I face this error:
ERROR: column "print_status" is of type print_statuses but expression is of type text
LINE 18: CASE
^
HINT: You will need to rewrite or cast the expression.
SQL state: 42804
Character: 477
print_statuses is an enum containing ('done', 'doing', 'not done'). And the print_status column of the metadata table is of type print_statuses (it also defaults to 'not done', so I wouldn't need the ELSE part anymore I guess).
It seems I do have to type cast the 'not done' text in the ELSE part of the CASE statement like this for this query to work:
UPDATE public.metadata m
SET print_status =
CASE
WHEN p.print IS NOT NULL THEN 'done'
ELSE 'not done'::print_statuses
END
FROM public.people p
WHERE m.fk_people_id = p.id;
Why do I have to do a type cast here, and not when using a simple UPDATE statement as in:
UPDATE public.metadata m SET print_status = 'not done' FROM public.people p WHERE g.fk_people_id = i.id;
?
I am trying to update jsonb column in java with mybatis.
Following is my mapper method
#Update("update service_user_assn set external_group = external_group || '{\"service_name\": \"#{service_name}\" }' where user=#{user} " +
" and service_name= (select service_name from services where service_name='Google') " )
public int update(#Param("service_name")String service_name,#Param("user") Integer user);
I am getting the following error while updating the jsonb (external_group) cloumn.
### Error updating database. Cause: org.postgresql.util.PSQLException: The column index is out of range: 2, number of columns: 1.
### The error may involve com.apds.mybatis.mapper.ServiceUserMapper.update-Inline
I am able to update with the same way for non-jsonb columns.
Also if I am putting hardcoded value it's working for jsonb columns.
How to solve this error while updating jsonb column?
You should not enclose #{} in single quotes because it will become part of a literal rather than a placeholder. i.e.
external_group = external_group || '{"service_name": "?"}' where ...
So, there will be only one placeholder in the PreparedStatement and you get the error.
The correct way is to concatenate the #{} in SQL.
You may also need to cast the literal to jsonb type explicitly.
#Update({
"update service_user_assn set",
"external_group = external_group",
"|| ('{\"service_name\": \"' || #{service_name} || '\" }')::jsonb",
"where user=#{user} and",
"service_name= (select service_name from services where service_name='Google')"})
The SQL being executed would look as follows.
external_group = external_group || ('{"service_name": "' || ? || '"}')::jsonb where ...
I want to insert a query string into a Postgres database column in the following format
{"enrolled_time":'''SELECT DISTINCT enrolled_time AT TIME ZONE %s FROM alluser'''}
I try this:
UPDATE reports SET raw_query = {"enrolled_time":'''SELECT DISTINCT enrolled_time AT TIME ZONE %s FROM alluser'''} WHERE id=37;
It gives error like
ERROR: syntax error at or near "{"
LINE 1: UPDATE base_reports SET extra_query = {"enrolled_time":'''SE...
When I try using single quotes it throws error like following:
ERROR: syntax error at or near "SELECT"
LINE 1: ...DATE reports SET raw_query = '{"enrolled_time":'''SELECT DIS...
How can I overcome this situation
Use dollar quoting:
UPDATE reports
SET raw_query = $${"enrolled_time":'''SELECT DISTINCT enrolled_time AT TIME ZONE %s FROM alluser'''}$$
WHERE id = 37;