0

I want to insert some testing data into my Customers table. I am using Postgresql and I am trying to do this using a for loop.

When I insert the data only once, it works just fine:

do $$
    declare fName varchar;
    declare lName varchar;
    begin
    fName := random_first_name();
    lName := random_last_name();

    insert into "Customers" (customer_first_name, customer_last_name, customer_mail, customer_age, customer_date_of_birth)
    select fName,
           lName,
           fName || '.' || lName || '@gmail.com',
           floor(random() * 85 + 1),
           cast(floor(random() * (2020-1950)+1950) || '-' || random_month() || '-' || floor(random()*30+1) as date);
end $$;

but when I try this using a for loop for 100 random customers, the table stays empty.

do $$
    declare fName varchar;
    declare lName varchar;
    begin
        for r in 1...100
            loop
            fName := random_first_name();
            lName := random_last_name();
            insert into "Customers" (customer_first_name, customer_last_name, customer_mail, customer_age, customer_date_of_birth)
            select fName,
                   lName,
                   fName || '.' || lName || '@gmail.com',
                   floor(random() * 85 + 1),
                   cast(floor(random() * (2020-1950)+1950) || '-' || random_month() || '-' || floor(random()*30+1) as date);
            end loop;
end $$;

Any tips for what I am doing wrong?

0 Answers0