Reputation: 75
I need to insert a large number of data sets in a table and want to use a procedure. So i want to use a for loop and do an insert each loop. But I have to insert a "to_timestamp('18.04.14 12:00:00'), which has to decrease one second each loop.
The insert looks like this:
insert into sv_value(value, timestamp)
values (40, to_timestamp('18.04.14 12:00:00'))
How should the procedure look like? Or is there another way to insert 100 Mio data sets?
Thank in advance...
Upvotes: 0
Views: 847
Reputation: 588
You can easily subtract time from a timestamp by using numbers. 1 is a day, 1/24 is an hour and so on:
procedure insert_data
is
l_timestamp timestamp;
begin
l_timestamp := to_timestamp('18.04.14 12:00:00');
for ... loop
insert into sv_value(value, timestamp)
values (40, l_timestamp);
l_timestamp := l_timestamp - 1 / 24 / 60 / 60;
end;
end;
However, a PL/SQL loop will take forever for 100 mio rows. It would be better to create a small set of data and the duplicate it several times. Duplication could look like this:
insert into sv_value(value, timestamp)
select value, timestamp - 5000 / 24 / 60 / 60
from sv_value;
It doesn't just duplicate the data. It adds an offset to the timestamp as well. If the offset is chosen carefully, you'll get all unique rows.
Upvotes: 1