My task is to insert data into an existing table.
My table is called user_entity
. Columns:
id (PK) 36 example - "a0a16d3f-ad38-491d-a98d-5af80428e139"
email
email_constraint
email_verified
enabled (true/false)
first_name
last_name
realm_id
username
created_timestamp
not_before
I want to write a loop to insert different data into the table.
Below you can see my bad procedure.
I want to generate unique id
and other data, e.g. Roman001, Chovgun001, email, and the value should increase (+1). But I don’t know how to do it, because these columns are not integer.
Where did I make mistakes? Is it possible to do?
CREATE PROCEDURE create_cs_kc_users()
LANGUAGE plpgsql
AS $procedure$
BEGIN
DECLARE
usersTotalCount := 100;
n := 0;
while n <= usersTotalCount loop
insert into edu_power_kc.user_entity (id, email, email_constraint, email_verified, enabled, first_name, last_name, realm_id, username, created_timestamp, not_before)
values (a0a16d3f-ad38-491d-a98d-5af80428e139, [email protected], [email protected], false, true, Roman001, Chovgun001, EduPowerKeycloak, romanchovgun001, 1623746793274, 0)
n := n + 1;
END
$procedure$
;
2
Answers
It works with next code Thanks a lot for help!
Your primary key seems to be a character data type (rather than
uuid
, which would be the proper data type), so you could generate values with thegen_random_uuid()
function and type cast the result totext
:Note that string constants have to be surrounded by single quotes, like
'[email protected]'
.