Amarundo
Amarundo

Reputation: 2397

How to bulk INSERT to a table with a loop in Postgres

To test a report against data, I want to INSERT test data into a table in Postgres SQL.

For example:

INSERT INTO call_logs (phonenumber,timeofcall) VALUES ('+12121001001','2014-12-23T07:01:00.000+00:00')

But I want to loop, say, 59 times, to have phone numbers 2121001001, 1002, 1003, etc with times of call 07:01, 07:02, 07:03, etc.

How do I do that with a loop?

Thanks.

Upvotes: 0

Views: 1404

Answers (1)

Clodoaldo Neto
Clodoaldo Neto

Reputation: 125204

You do not need a loop. Use generate_series:

insert into call_logs (column1, phonenumber,timeofcall)
select
    'constant_value',
    '+' || generate_series(12121001001, 12121001059),
    generate_series(
        '2014-12-23t07:01:00.000+00:00'::timestamp,
        '2014-12-23t07:59:00.000+00:00',
        '1 minute'
    )

Upvotes: 3

Related Questions