Using postgres and plpgsql, I'm trying to create a...
# help
l
Using postgres and plpgsql, I'm trying to create a function with takes in a parameter which is an array of objects.
Copy code
sql
CREATE FUNCTION test_function(products_object_array_input some_data_type) -- what should the data type be? array? jsonb[]?
An array of objects that I would pass into the function could look like this:
Copy code
json
[
  { "productID": 3, "quantity": 5 },
  { "productID": 1, "quantity": 2 }
]
First question: What should I set the products_object_array_input data type as? array? jsonb[]? Second question: I then want to loop over the array to insert the values of the object array. Again, using plpgsql. How?
Copy code
sql
-- I want to loop over cart_array_input and do this for every object in the array. Sorry for pseudo-code. i = iterator
insert into order_item(product_id, quantity)
values(products_object_array_input[i].productID, products_object_array_input[i].quantity);
n
Hello @Ludvig! This thread has been automatically created from your message in #843999948717555735 a few seconds ago. We have already mentioned the @User so that they can see your message and help you as soon as possible! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ``...`` menu) and select "Leave Thread" to unsubscribe from future updates. Want to change the title? Use the ``/title`` command! We have solved your problem? Click the button below to archive it.
l
My apologies, realised this should be in #869405720934744086
o
Hi Actually both are good, but yes you will probably have better answers in the #869405720934744086 channel 🙂 @Ludvig
n
Ludvig (2022-05-09)