0

I'm new to PostgreSQL and before I worked on SQL Server. I want to create a table with a key and a JSON column to write and update my JSON field. I use the jsonb_set method in PostgreSQL.

For example

update "Comment"
set "testJsonb" = jsonb_set("testJsonb",'{uuid}',SomeMethod,true)

I want to replace this function uuid_generate_v4() with SomeMethod. I figured that I should not put function there, then I decided to put a variable there, but I can't do that and I don't know why.

DECLARE cnt varchar;

as you see above code doesn't work

DO $$
DECLARE  
   myVar VARCHAR := 'myValue';   
BEGIN  
select myVar
END $$;

another way

CREATE OR REPLACE FUNCTION clean_emp()
RETURNS void AS $$
DECLARE cnt varchar;
BEGIN
END;
$$ LANGUAGE plpgsql;

I can declare a variable in a function, but I don't want this because I can't put a function in SomeMethod in jsonb_set. Am I wrong? Or should this be done in another way?

mohazh
  • 1
  • 2
  • You need a destination for the select statement. You cannot just dump it to nowhere. Use "raise" to generate output to console https://stackoverflow.com/questions/23465429/printing-a-value-of-a-variable-in-postgresql As for variable usage here is a link with more links inside: https://stackoverflow.com/questions/766657/how-do-you-use-variables-in-a-simple-postgresql-script – Bjarni Ragnarsson Dec 28 '22 at 10:30

0 Answers0