Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert data in postgresql using for loop?

Tags:

sql

postgresql

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..

like image 396
Pablo Job Avatar asked Dec 14 '16 01:12

Pablo Job


People also ask

Can we use for loop in PostgreSQL?

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.

How do I add multiple values in PostgreSQL?

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.


1 Answers

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; $$; 
like image 144
Riya Bansal Avatar answered Sep 27 '22 21:09

Riya Bansal