I would like to know how to insert data in postgresql using for loop?
I want to insert 1 to 1000 in row of id..
For loop to iterate over a range of integers An integer variable loop_cnt is created at first, which is accessible inside the loop only. After each iteration, the for loop adds the step to the loop_cnt. However, when we use the reverse option, the for loop subtracts the step from loop_cnt after each iteration.
PostgreSQL INSERT Multiple Rows First, specify the name of the table that you want to insert data after the INSERT INTO keywords. Second, list the required columns or all columns of the table in parentheses that follow the table name. Third, supply a comma-separated list of rows after the VALUES keyword.
Please try below code to insert values in tables using for loop.
do $$ begin for r in 1..1000 loop insert into schema_name.table_name(id) values(r); end loop; end; $$;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With