place_pk | placename |
---|---|
123 | place1 |
125 | place2 |
127 | place3 |
129 | place4 |
131 | place5 |
133 | plcae6 |
The main idea was to do the generation of the new table in a for cycle. First step is to determine what variables are needed for the operation.
we needed the current status of the sequence counter, and we have named it p_count. Then we needed the ids of the place on which we iterated, so we named it placeid. All these two were of numeric types.
DECLARE p_count numeric; placeid numeric;The second step was to create a for loop:
FOR placeid IN SELECT place_pk FROM place LOOP n_count=n_count+1; INSERT INTO lease_interval (lease_interval_id, leasetime, place) SELECT p_count, NULL, placeid; END LOOP;
Withe the help of the query
select (i::date + '1 day'::interval) from generate_series('2012-12-30', '2013-01-05', '1 day'::interval) i;Here, the sequence is incremented in every iteration, so we could keep track of the incrementing of the sequence.
then using the SELECT place_pk FROM place statement, we could select all the values of place_pk, and iterate through them.
Then the insert statement is just the usual, with the exception of using the variables declared in the function declaration section.
After the loop is finished, the sequence is updated with the new sequence count.
The function creation and the transaction handling is as usual, as you can see the whole script:
begin; CREATE OR REPLACE FUNCTION add_default_lease_time() RETURNS void AS $$ DECLARE p_count numeric; placeid numeric; BEGIN select seq_count from sequence INTO p_count; FOR placeid IN SELECT place_pk FROM place LOOP n_count=n_count+1; INSERT INTO lease_interval (lease_interval_id, leasetime, place) SELECT p_count, NULL, placeid; END LOOP; update sequence set seq_count= p_count; END; $$ LANGUAGE plpgsql; select * from add_default_lease_time(); commit;
And the results:
lease_interval_id | leasetime | place |
---|---|---|
221 | null | 123 |
222 | null | 125 |
223 | null | 127 |
224 | null | 129 |
225 | null | 131 |
226 | null | 133 |
No comments:
Post a Comment