Quantcast
Viewing latest article 3
Browse Latest Browse All 3

Use array of composite type as function parameter and access it

I have created a type Books in Postgres which has 2 numeric fields and 2 varchar fields. I want to send an array of Books to a function to INSERT those values in a matching table.

This is my type:

CREATE TYPE Books AS (V_Book_ID NUMERIC,V_Row_Num NUMERIC,V_Book_OWNER TEXT,V_Book_OWNER_ID TEXT);

This is my function:

CREATE OR REPLACE FUNCTION Update_Table(row_book Books[]) RETURNS TEXT AS $$DECLARE   Status TEXT;   I_Max integer := array_length(row_book, 1);BEGINFOR I in 1..I_Max  LOOP   INSERT INTO books_table(Book_ID,   Row_Num,   Book_OWNER,   Book_OWNER_ID)   values   (row_book[I].V_Book_ID,   row_book[I].V_Row_Num,   row_book[I].V_Book_OWNER,   row_book[I].V_Book_OWNER_ID);END LOOP;   STATUS:='Saved';exception when others then   STATUS:='failure';   RETURN STATUS;END;$$ language plpgsql;

How do I send data to the function or how should I call the function with data?


Viewing latest article 3
Browse Latest Browse All 3

Trending Articles