In one of my SQL scripts for PostgreSQL I want to declare a value so that I can use it in several places in the rest of the script.
Here is what I did based on the following thread :
\set cat_id uuid_generate_v4()
insert into categories (id, name) values (:cat_id, 'Category 1')
insert into other_table (id, category_id) values (uuid_generate_v4(), :cat_id)
The problem is that the variable cat_id
does not take the value once for all. It just replaces :cat_id
by uuid_generate_v4()
. Therefore, the value is not the same in the two insert queries.
How can I do to give cat_id
the value of the execution of the function ?
Thanks.