2 years ago

#68147

test-img

Yohanes Lim

Postgresql Function for Inserting Data With Join

i tried to do create postgres sql function to insert data from parameter that given in the function.

For example There are 2 tables: Table Customer

Customer
customer_id
customer_name
customer_phone

Table Transaction

seller_id
customer_name
customer_phone
price
item_name

My current function looks like:

CREATE OR REPLATE FUNCTON add_transaction_detail(seller_id int, customer_id int, price int, item_name varchar)
RETURNS character varying
LANGUAGE plpsql
SECURITY DEFINER
AS $function$

DECLARE 
seller_id ALIAS FOR $1;
customer_id ALIAS FOR $2;

BEGIN

INSERT INTO transaction(seller_id, customer_name, customer_phone, item_name, price) 
SELECT seller_id, c.customer_name, c.customer_phone, item_name, price
FROM table1 t JOIN customer c ON t.customer_id=c.customer_id

END;
$function$

i found error in my insert code. is there any way to set the parameter into 1 table then join them ? thanks

sql

postgresql

plpgsql

0 Answers

Your Answer

Accepted video resources