in my postgre SQL statement I need to concat an ID int with string and insert it as JSON value into JSONB column. My query is as follows:
DO
$$
DECLARE
myID int = 555;
BEGIN
INSERT INTO mytable (status, url)
VALUES(0,
'{
"alias": "XXX",
"pageUrl": "https://example.com/?myID="' || myID || '}'::jsonb
);
END
$$;
But this query fails with error ERROR: syntax error at or near "INSERT"
. Any ideas how to fix it would be welcome. Thank you.
2
Answers
Here is a way to do it using
json_build_object
to prepare your json :You can try this option
Schema (PostgreSQL v15)
Query #1
View on DB Fiddle