2

I have a function that takes 3 values:

partitionId varchar(48)='tenant';
jobId varchar(40)='job1';
taskIds varchar[] := '{job1.1, job1.5, job1.3}';

I would like to have those inserted in a table as:

tenant  job1    job1.1  2020-10-09 11:38:03
tenant  job1    job1.5  2020-10-09 11:38:03
tenant  job1    job1.3  2020-10-09 11:38:03

I know that I could use a loop on the array then insert one by one, but I believe it's not the most efficient so, I am trying to insert in one go. Not sure if I should use an intermediary table for that or if there's a more direct solution.

I tried:

DO
$do$
DECLARE
  
    partitionId varchar(48)='tenant';
    jobId varchar(40)='job1';
    taskIds varchar[] := '{job1.1, job1.5, job1.3}';
begin
    
    insert into completed_subtask_report values (partitionId, jobId, taskIds, current_timestamp );
    
END
$do$

But that would insert the array as:

tenant  job1    {job1.1,job1.5,job1.3}  2020-10-09 12:44:09

Anyone knows how to do that?

2
  • If you have received a satisfactory answer please accept it. Please do not leave answered questions as unanswered. Commented Oct 12, 2020 at 3:23
  • sorry, my bad, just did.Thx! Commented Oct 12, 2020 at 15:16

1 Answer 1

3

Use unnest:

insert into completed_subtask_report ( ... column names go here ...)
select 'tenant', 'job1', x.task, current_timestamp
from unnest('{job1.1, job1.5, job1.3}') as x(task)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.