Insert array as new rows in postgresql without a loop

Xbreizh

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?

a_horse_with_no_name

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Insert array into hashmap without creating a new array

From Dev

loop and insert into another array without redundancy

From Dev

searching an element in an array without a loop in postgresql

From Dev

Truncate rows and insert new ones without introducing a service break?

From Dev

Postgresql Trigger to insert rows

From Dev

How to insert new Elements into a multidimensional array without push_array?

From Dev

How To Use insert array's data into different rows using For Loop

From Dev

How to insert an array into the database without inserting every loop in php

From Dev

Insert a group of rows to a table in PostgreSQL

From Dev

Postgresql Insert select with multiple rows

From Dev

PostgreSQL INSERT into an array of enums

From Dev

From a scratch table, insert ONLY rows that don't exist in a new table. PostgreSQL

From Dev

Insert new rows in pandas dataframe

From Dev

Apex button to insert new rows

From Dev

Apex button to insert new rows

From Dev

insert rows with a loop in mysql query

From Dev

Insert multiple rows using for loop

From Dev

For Loop to insert multiple rows in SQL

From Dev

Loop through rows and INSERT multiple rows into MySQL

From Dev

Insert new array into multidimensional array

From Dev

Insert an array in an array using loop

From Dev

Insert new array and summing the results of the new array

From Dev

INSERT multiple rows with SELECT and an array

From Dev

How to insert an array into different rows?

From Dev

Insert multiple rows where not exists PostgresQL

From Dev

How to insert multiple rows using a function in PostgreSQL

From Dev

PostgreSQL Return affected rows by UPDATE/INSERT query

From Dev

How to insert multiple rows using a function in PostgreSQL

From Dev

Postgresql select rows(a result) as array

Related Related

  1. 1

    Insert array into hashmap without creating a new array

  2. 2

    loop and insert into another array without redundancy

  3. 3

    searching an element in an array without a loop in postgresql

  4. 4

    Truncate rows and insert new ones without introducing a service break?

  5. 5

    Postgresql Trigger to insert rows

  6. 6

    How to insert new Elements into a multidimensional array without push_array?

  7. 7

    How To Use insert array's data into different rows using For Loop

  8. 8

    How to insert an array into the database without inserting every loop in php

  9. 9

    Insert a group of rows to a table in PostgreSQL

  10. 10

    Postgresql Insert select with multiple rows

  11. 11

    PostgreSQL INSERT into an array of enums

  12. 12

    From a scratch table, insert ONLY rows that don't exist in a new table. PostgreSQL

  13. 13

    Insert new rows in pandas dataframe

  14. 14

    Apex button to insert new rows

  15. 15

    Apex button to insert new rows

  16. 16

    insert rows with a loop in mysql query

  17. 17

    Insert multiple rows using for loop

  18. 18

    For Loop to insert multiple rows in SQL

  19. 19

    Loop through rows and INSERT multiple rows into MySQL

  20. 20

    Insert new array into multidimensional array

  21. 21

    Insert an array in an array using loop

  22. 22

    Insert new array and summing the results of the new array

  23. 23

    INSERT multiple rows with SELECT and an array

  24. 24

    How to insert an array into different rows?

  25. 25

    Insert multiple rows where not exists PostgresQL

  26. 26

    How to insert multiple rows using a function in PostgreSQL

  27. 27

    PostgreSQL Return affected rows by UPDATE/INSERT query

  28. 28

    How to insert multiple rows using a function in PostgreSQL

  29. 29

    Postgresql select rows(a result) as array

HotTag

Archive